Add word count visitor

This commit is contained in:
Tim Van Baak 2020-03-05 21:08:36 -08:00
parent f684695836
commit e08fcefb5f
4 changed files with 30 additions and 5 deletions

View File

@ -3,4 +3,4 @@ Module encapsulating all markdown parsing functionality
"""
from amanuensis.parser.text import parse_raw_markdown
from amanuensis.parser.render import PreviewHtmlRenderer
from amanuensis.parser.render import PreviewHtmlRenderer, WordCounter

View File

@ -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'<i>{"".join(span.recurse(self))}</i>'
def CitationSpan(self, span):
return f'<a href="#">{span.cite_text}</a>'
return f'<a href="#">{"".join(span.recurse(self))}</a>'
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))

View File

@ -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));

View File

@ -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