Add word count visitor
This commit is contained in:
parent
f684695836
commit
e08fcefb5f
|
@ -3,4 +3,4 @@ Module encapsulating all markdown parsing functionality
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from amanuensis.parser.text import parse_raw_markdown
|
from amanuensis.parser.text import parse_raw_markdown
|
||||||
from amanuensis.parser.render import PreviewHtmlRenderer
|
from amanuensis.parser.render import PreviewHtmlRenderer, WordCounter
|
|
@ -3,6 +3,8 @@ Internal module encapsulating the render logic for parsed articles. Rendering
|
||||||
is done via a rough approximation of the visitor pattern.
|
is done via a rough approximation of the visitor pattern.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
class PreviewHtmlRenderer():
|
class PreviewHtmlRenderer():
|
||||||
def TextSpan(self, span):
|
def TextSpan(self, span):
|
||||||
|
@ -22,4 +24,22 @@ class PreviewHtmlRenderer():
|
||||||
def ItalicSpan(self, span):
|
def ItalicSpan(self, span):
|
||||||
return f'<i>{"".join(span.recurse(self))}</i>'
|
return f'<i>{"".join(span.recurse(self))}</i>'
|
||||||
def CitationSpan(self, span):
|
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))
|
||||||
|
|
|
@ -46,7 +46,8 @@ function update(article) {
|
||||||
req.onreadystatechange = function () {
|
req.onreadystatechange = function () {
|
||||||
if (req.readyState == 4 && req.status == 200) {
|
if (req.readyState == 4 && req.status == 200) {
|
||||||
// params.article = article;
|
// 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));
|
req.send(JSON.stringify(article));
|
||||||
|
|
|
@ -8,7 +8,7 @@ from amanuensis.config import root
|
||||||
from amanuensis.config.loader import ReadOnlyOrderedDict
|
from amanuensis.config.loader import ReadOnlyOrderedDict
|
||||||
from amanuensis.errors import MissingConfigError
|
from amanuensis.errors import MissingConfigError
|
||||||
from amanuensis.lexicon.manage import valid_add, add_player, add_character
|
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 (
|
from amanuensis.server.forms import (
|
||||||
LexiconConfigForm, LexiconJoinForm,LexiconCharacterForm)
|
LexiconConfigForm, LexiconJoinForm,LexiconCharacterForm)
|
||||||
from amanuensis.server.helpers import (
|
from amanuensis.server.helpers import (
|
||||||
|
@ -239,12 +239,16 @@ def get_bp():
|
||||||
# TODO verification
|
# TODO verification
|
||||||
parsed_draft = parse_raw_markdown(article['contents'])
|
parsed_draft = parse_raw_markdown(article['contents'])
|
||||||
rendered_html = parsed_draft.render(PreviewHtmlRenderer())
|
rendered_html = parsed_draft.render(PreviewHtmlRenderer())
|
||||||
|
word_count = parsed_draft.render(WordCounter())
|
||||||
|
|
||||||
filename = f'{article["character"]}.{article["aid"]}'
|
filename = f'{article["character"]}.{article["aid"]}'
|
||||||
with g.lexicon.ctx.draft.edit(filename) as a:
|
with g.lexicon.ctx.draft.edit(filename) as a:
|
||||||
a.update(article)
|
a.update(article)
|
||||||
|
|
||||||
# TODO return more info
|
# TODO return more info
|
||||||
return {'rendered': rendered_html}
|
return {
|
||||||
|
'rendered': rendered_html,
|
||||||
|
'word_count': word_count,
|
||||||
|
}
|
||||||
|
|
||||||
return bp
|
return bp
|
||||||
|
|
Loading…
Reference in New Issue