Add word count

This commit is contained in:
Tim Van Baak 2020-03-05 22:37:01 -08:00
parent a7f2a56345
commit 7780d2b1f1
5 changed files with 44 additions and 14 deletions

View File

@ -2,6 +2,6 @@
Module encapsulating all markdown parsing functionality
"""
from amanuensis.parser.analyze import WordCounter
from amanuensis.parser.analyze import FeatureCounter
from amanuensis.parser.text import parse_raw_markdown
from amanuensis.parser.render import PreviewHtmlRenderer

View File

@ -5,20 +5,41 @@ for verification against constraints.
import re
class WordCounter():
class FeatureCounter():
def __init__(self):
self.word_count = 0
self.citation_count = 0
self.has_signature = False
def TextSpan(self, span):
return len(re.split('\s+', span.innertext.strip()))
self.word_count += len(re.split('\s+', span.innertext.strip()))
return self
def LineBreak(self, span):
return 0
return self
def ParsedArticle(self, span):
return sum(span.recurse(self))
span.recurse(self)
return self
def BodyParagraph(self, span):
return sum(span.recurse(self))
span.recurse(self)
return self
def SignatureParagraph(self, span):
return sum(span.recurse(self))
self.has_signature = True
span.recurse(self)
return self
def BoldSpan(self, span):
return sum(span.recurse(self))
span.recurse(self)
return self
def ItalicSpan(self, span):
return sum(span.recurse(self))
span.recurse(self)
return self
def CitationSpan(self, span):
return sum(span.recurse(self))
self.citation_count += 1
span.recurse(self)
return self

View File

@ -7,19 +7,26 @@ readable formats.
class PreviewHtmlRenderer():
def TextSpan(self, span):
return span.innertext
def LineBreak(self, span):
return '<br>'
def ParsedArticle(self, span):
return '\n'.join(span.recurse(self))
def BodyParagraph(self, span):
return f'<p>{"".join(span.recurse(self))}</p>'
def SignatureParagraph(self, span):
return ('<hr><span class="signature"><p>'
f'{"".join(span.recurse(self))}'
'</p></span>')
def BoldSpan(self, span):
return f'<b>{"".join(span.recurse(self))}</b>'
def ItalicSpan(self, span):
return f'<i>{"".join(span.recurse(self))}</i>'
def CitationSpan(self, span):
return f'<a href="#">{"".join(span.recurse(self))}</a>'

View File

@ -46,7 +46,9 @@ function update(article) {
req.onreadystatechange = function () {
if (req.readyState == 4 && req.status == 200) {
// params.article = article;
document.getElementById("preview").innerHTML = req.response.rendered;
var title = document.getElementById("editor-title").value;
var previewHtml = "<h1>" + title + "</h1>\n" + req.response.rendered
document.getElementById("preview").innerHTML = previewHtml;
document.getElementById("preview-control").innerHTML = req.response.word_count;
}
};

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, WordCounter
from amanuensis.parser import parse_raw_markdown, PreviewHtmlRenderer, FeatureCounter
from amanuensis.server.forms import (
LexiconConfigForm, LexiconJoinForm,LexiconCharacterForm)
from amanuensis.server.helpers import (
@ -239,7 +239,7 @@ def get_bp():
# TODO verification
parsed_draft = parse_raw_markdown(article['contents'])
rendered_html = parsed_draft.render(PreviewHtmlRenderer())
word_count = parsed_draft.render(WordCounter())
features = parsed_draft.render(FeatureCounter())
filename = f'{article["character"]}.{article["aid"]}'
with g.lexicon.ctx.draft.edit(filename) as a:
@ -248,7 +248,7 @@ def get_bp():
# TODO return more info
return {
'rendered': rendered_html,
'word_count': word_count,
'word_count': features.word_count,
}
return bp