From a7f2a56345f1da75e173a7885237783d97c1b706 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Thu, 5 Mar 2020 22:03:04 -0800 Subject: [PATCH] Move word counter to analyze submodule --- amanuensis/parser/__init__.py | 3 ++- amanuensis/parser/analyze.py | 24 ++++++++++++++++++++++++ amanuensis/parser/render.py | 24 ++---------------------- 3 files changed, 28 insertions(+), 23 deletions(-) create mode 100644 amanuensis/parser/analyze.py diff --git a/amanuensis/parser/__init__.py b/amanuensis/parser/__init__.py index 1b89b90..7c39cf5 100644 --- a/amanuensis/parser/__init__.py +++ b/amanuensis/parser/__init__.py @@ -2,5 +2,6 @@ Module encapsulating all markdown parsing functionality """ +from amanuensis.parser.analyze import WordCounter from amanuensis.parser.text import parse_raw_markdown -from amanuensis.parser.render import PreviewHtmlRenderer, WordCounter \ No newline at end of file +from amanuensis.parser.render import PreviewHtmlRenderer \ No newline at end of file diff --git a/amanuensis/parser/analyze.py b/amanuensis/parser/analyze.py new file mode 100644 index 0000000..f503463 --- /dev/null +++ b/amanuensis/parser/analyze.py @@ -0,0 +1,24 @@ +""" +Internal module encapsulating visitors that compute metrics on articles +for verification against constraints. +""" + +import re + +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/parser/render.py b/amanuensis/parser/render.py index 7cddcc8..3beb4b5 100644 --- a/amanuensis/parser/render.py +++ b/amanuensis/parser/render.py @@ -1,10 +1,8 @@ """ -Internal module encapsulating the render logic for parsed articles. Rendering -is done via a rough approximation of the visitor pattern. +Internal module encapsulating visitors that render articles into +readable formats. """ -import re - class PreviewHtmlRenderer(): def TextSpan(self, span): @@ -25,21 +23,3 @@ class PreviewHtmlRenderer(): return f'{"".join(span.recurse(self))}' def CitationSpan(self, span): 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))