From 7780d2b1f1365f8427a6a9fe0be8fd38e5110daf Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Thu, 5 Mar 2020 22:37:01 -0800 Subject: [PATCH] Add word count --- amanuensis/parser/__init__.py | 2 +- amanuensis/parser/analyze.py | 39 ++++++++++++++++++++++++++-------- amanuensis/parser/render.py | 7 ++++++ amanuensis/resources/editor.js | 4 +++- amanuensis/server/lexicon.py | 6 +++--- 5 files changed, 44 insertions(+), 14 deletions(-) diff --git a/amanuensis/parser/__init__.py b/amanuensis/parser/__init__.py index 7c39cf5..a7424ae 100644 --- a/amanuensis/parser/__init__.py +++ b/amanuensis/parser/__init__.py @@ -2,6 +2,6 @@ Module encapsulating all markdown parsing functionality """ -from amanuensis.parser.analyze import WordCounter +from amanuensis.parser.analyze import FeatureCounter from amanuensis.parser.text import parse_raw_markdown from amanuensis.parser.render import PreviewHtmlRenderer \ No newline at end of file diff --git a/amanuensis/parser/analyze.py b/amanuensis/parser/analyze.py index f503463..3917c3b 100644 --- a/amanuensis/parser/analyze.py +++ b/amanuensis/parser/analyze.py @@ -5,20 +5,41 @@ for verification against constraints. import re -class WordCounter(): +class FeatureCounter(): + def __init__(self): + self.word_count = 0 + self.citation_count = 0 + self.has_signature = False + def TextSpan(self, span): - return len(re.split('\s+', span.innertext.strip())) + self.word_count += len(re.split('\s+', span.innertext.strip())) + return self + def LineBreak(self, span): - return 0 + return self + def ParsedArticle(self, span): - return sum(span.recurse(self)) + span.recurse(self) + return self + def BodyParagraph(self, span): - return sum(span.recurse(self)) + span.recurse(self) + return self + def SignatureParagraph(self, span): - return sum(span.recurse(self)) + self.has_signature = True + span.recurse(self) + return self + def BoldSpan(self, span): - return sum(span.recurse(self)) + span.recurse(self) + return self + def ItalicSpan(self, span): - return sum(span.recurse(self)) + span.recurse(self) + return self + def CitationSpan(self, span): - return sum(span.recurse(self)) + self.citation_count += 1 + span.recurse(self) + return self diff --git a/amanuensis/parser/render.py b/amanuensis/parser/render.py index 3beb4b5..c6c00a4 100644 --- a/amanuensis/parser/render.py +++ b/amanuensis/parser/render.py @@ -7,19 +7,26 @@ readable formats. class PreviewHtmlRenderer(): def TextSpan(self, span): return span.innertext + def LineBreak(self, span): return '
' + def ParsedArticle(self, span): return '\n'.join(span.recurse(self)) + def BodyParagraph(self, span): return f'

{"".join(span.recurse(self))}

' + def SignatureParagraph(self, span): return ('

' f'{"".join(span.recurse(self))}' '

') + def BoldSpan(self, span): return f'{"".join(span.recurse(self))}' + def ItalicSpan(self, span): return f'{"".join(span.recurse(self))}' + def CitationSpan(self, span): return f'{"".join(span.recurse(self))}' diff --git a/amanuensis/resources/editor.js b/amanuensis/resources/editor.js index d1d9978..a8c77fa 100644 --- a/amanuensis/resources/editor.js +++ b/amanuensis/resources/editor.js @@ -46,7 +46,9 @@ function update(article) { req.onreadystatechange = function () { if (req.readyState == 4 && req.status == 200) { // params.article = article; - document.getElementById("preview").innerHTML = req.response.rendered; + var title = document.getElementById("editor-title").value; + var previewHtml = "

" + title + "

\n" + req.response.rendered + document.getElementById("preview").innerHTML = previewHtml; document.getElementById("preview-control").innerHTML = req.response.word_count; } }; diff --git a/amanuensis/server/lexicon.py b/amanuensis/server/lexicon.py index a940c34..f902071 100644 --- a/amanuensis/server/lexicon.py +++ b/amanuensis/server/lexicon.py @@ -8,7 +8,7 @@ from amanuensis.config import root from amanuensis.config.loader import ReadOnlyOrderedDict from amanuensis.errors import MissingConfigError from amanuensis.lexicon.manage import valid_add, add_player, add_character -from amanuensis.parser import parse_raw_markdown, PreviewHtmlRenderer, WordCounter +from amanuensis.parser import parse_raw_markdown, PreviewHtmlRenderer, FeatureCounter from amanuensis.server.forms import ( LexiconConfigForm, LexiconJoinForm,LexiconCharacterForm) from amanuensis.server.helpers import ( @@ -239,7 +239,7 @@ def get_bp(): # TODO verification parsed_draft = parse_raw_markdown(article['contents']) rendered_html = parsed_draft.render(PreviewHtmlRenderer()) - word_count = parsed_draft.render(WordCounter()) + features = parsed_draft.render(FeatureCounter()) filename = f'{article["character"]}.{article["aid"]}' with g.lexicon.ctx.draft.edit(filename) as a: @@ -248,7 +248,7 @@ def get_bp(): # TODO return more info return { 'rendered': rendered_html, - 'word_count': word_count, + 'word_count': features.word_count, } return bp