From e08fcefb5f0dd6a634da367da62437d5d3373303 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Thu, 5 Mar 2020 21:08:36 -0800 Subject: [PATCH] Add word count visitor --- amanuensis/parser/__init__.py | 2 +- amanuensis/parser/render.py | 22 +++++++++++++++++++++- amanuensis/resources/editor.js | 3 ++- amanuensis/server/lexicon.py | 8 ++++++-- 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/amanuensis/parser/__init__.py b/amanuensis/parser/__init__.py index a4bccb3..1b89b90 100644 --- a/amanuensis/parser/__init__.py +++ b/amanuensis/parser/__init__.py @@ -3,4 +3,4 @@ Module encapsulating all markdown parsing functionality """ from amanuensis.parser.text import parse_raw_markdown -from amanuensis.parser.render import PreviewHtmlRenderer \ No newline at end of file +from amanuensis.parser.render import PreviewHtmlRenderer, WordCounter \ No newline at end of file diff --git a/amanuensis/parser/render.py b/amanuensis/parser/render.py index 1016fda..7cddcc8 100644 --- a/amanuensis/parser/render.py +++ b/amanuensis/parser/render.py @@ -3,6 +3,8 @@ Internal module encapsulating the render logic for parsed articles. Rendering is done via a rough approximation of the visitor pattern. """ +import re + class PreviewHtmlRenderer(): def TextSpan(self, span): @@ -22,4 +24,22 @@ class PreviewHtmlRenderer(): def ItalicSpan(self, span): return f'{"".join(span.recurse(self))}' def CitationSpan(self, span): - return f'{span.cite_text}' + return f'{"".join(span.recurse(self))}' + +class WordCounter(): + def TextSpan(self, span): + return len(re.split('\s+', span.innertext.strip())) + def LineBreak(self, span): + return 0 + def ParsedArticle(self, span): + return sum(span.recurse(self)) + def BodyParagraph(self, span): + return sum(span.recurse(self)) + def SignatureParagraph(self, span): + return sum(span.recurse(self)) + def BoldSpan(self, span): + return sum(span.recurse(self)) + def ItalicSpan(self, span): + return sum(span.recurse(self)) + def CitationSpan(self, span): + return sum(span.recurse(self)) diff --git a/amanuensis/resources/editor.js b/amanuensis/resources/editor.js index d81db22..d1d9978 100644 --- a/amanuensis/resources/editor.js +++ b/amanuensis/resources/editor.js @@ -46,7 +46,8 @@ function update(article) { req.onreadystatechange = function () { if (req.readyState == 4 && req.status == 200) { // params.article = article; - document.getElementById("preview-control").innerHTML = req.response.rendered; + document.getElementById("preview").innerHTML = req.response.rendered; + document.getElementById("preview-control").innerHTML = req.response.word_count; } }; req.send(JSON.stringify(article)); diff --git a/amanuensis/server/lexicon.py b/amanuensis/server/lexicon.py index f8fbe6f..a940c34 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 +from amanuensis.parser import parse_raw_markdown, PreviewHtmlRenderer, WordCounter from amanuensis.server.forms import ( LexiconConfigForm, LexiconJoinForm,LexiconCharacterForm) from amanuensis.server.helpers import ( @@ -239,12 +239,16 @@ def get_bp(): # TODO verification parsed_draft = parse_raw_markdown(article['contents']) rendered_html = parsed_draft.render(PreviewHtmlRenderer()) + word_count = parsed_draft.render(WordCounter()) filename = f'{article["character"]}.{article["aid"]}' with g.lexicon.ctx.draft.edit(filename) as a: a.update(article) # TODO return more info - return {'rendered': rendered_html} + return { + 'rendered': rendered_html, + 'word_count': word_count, + } return bp