From 7eadaa0db47641783bb917781ae7738f0414c738 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Wed, 9 Jun 2021 15:39:14 -0700 Subject: [PATCH] Move analysis visitors out of parser module --- amanuensis/lexicon/gameloop.py | 43 ++++++++++++++++++++++++++--- amanuensis/parser/__init__.py | 3 --- amanuensis/parser/analyze.py | 49 ---------------------------------- 3 files changed, 40 insertions(+), 55 deletions(-) delete mode 100644 amanuensis/parser/analyze.py diff --git a/amanuensis/lexicon/gameloop.py b/amanuensis/lexicon/gameloop.py index 1ce2072..3e51150 100644 --- a/amanuensis/lexicon/gameloop.py +++ b/amanuensis/lexicon/gameloop.py @@ -9,11 +9,48 @@ from amanuensis.config import ReadOnlyOrderedDict from amanuensis.models import LexiconModel, UserModel from amanuensis.parser import ( parse_raw_markdown, - GetCitations, HtmlRenderer, titlesort, - filesafe_title, - ConstraintAnalysis) + filesafe_title) +from amanuensis.parser.core import RenderableVisitor + + +class GetCitations(RenderableVisitor): + def __init__(self): + self.citations = [] + + def ParsedArticle(self, span): + span.recurse(self) + return self.citations + + def CitationSpan(self, span): + self.citations.append(span.cite_target) + return self + + +class ConstraintAnalysis(RenderableVisitor): + def __init__(self, lexicon: LexiconModel): + self.info: List[str] = [] + self.warning: List[str] = [] + self.error: List[str] = [] + + self.word_count: int = 0 + self.citations: list = [] + self.signatures: int = 0 + + def TextSpan(self, span): + self.word_count += len(re.split(r'\s+', span.innertext.strip())) + return self + + def SignatureParagraph(self, span): + self.signatures += 1 + span.recurse(self) + return self + + def CitationSpan(self, span): + self.citations.append(span.cite_target) + span.recurse(self) + return self def get_player_characters( diff --git a/amanuensis/parser/__init__.py b/amanuensis/parser/__init__.py index 1de2c5d..5ef2072 100644 --- a/amanuensis/parser/__init__.py +++ b/amanuensis/parser/__init__.py @@ -2,15 +2,12 @@ Module encapsulating all markdown parsing functionality. """ -from .analyze import ConstraintAnalysis, GetCitations from .core import normalize_title from .helpers import titlesort, filesafe_title from .parsing import parse_raw_markdown from .render import PreviewHtmlRenderer, HtmlRenderer __all__ = [ - ConstraintAnalysis.__name__, - GetCitations.__name__, normalize_title.__name__, titlesort.__name__, filesafe_title.__name__, diff --git a/amanuensis/parser/analyze.py b/amanuensis/parser/analyze.py deleted file mode 100644 index bf52354..0000000 --- a/amanuensis/parser/analyze.py +++ /dev/null @@ -1,49 +0,0 @@ -""" -Internal module encapsulating visitors that compute metrics on articles -for verification against constraints. -""" - -import re -from typing import List - -from amanuensis.models import LexiconModel - -from .core import RenderableVisitor - - -class GetCitations(RenderableVisitor): - def __init__(self): - self.citations = [] - - def ParsedArticle(self, span): - span.recurse(self) - return self.citations - - def CitationSpan(self, span): - self.citations.append(span.cite_target) - return self - - -class ConstraintAnalysis(RenderableVisitor): - def __init__(self, lexicon: LexiconModel): - self.info: List[str] = [] - self.warning: List[str] = [] - self.error: List[str] = [] - - self.word_count: int = 0 - self.citations: list = [] - self.signatures: int = 0 - - def TextSpan(self, span): - self.word_count += len(re.split(r'\s+', span.innertext.strip())) - return self - - def SignatureParagraph(self, span): - self.signatures += 1 - span.recurse(self) - return self - - def CitationSpan(self, span): - self.citations.append(span.cite_target) - span.recurse(self) - return self