Calculate wordcount better and make togglable
This commit is contained in:
parent
243ccc4641
commit
6f8aa6ee09
5 changed files with 28 additions and 3 deletions
|
@ -11,7 +11,9 @@
|
|||
Page <span class="page"></span> of <span class="topage"></span>
|
||||
</td>
|
||||
<td>
|
||||
{% if word_count %}
|
||||
Total Words: {{ word_count }}
|
||||
{% endif %}
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
from md_pdf.consts import TEMPLATES_DIR, STATIC_DIR
|
||||
from word_count import word_count
|
||||
from md_pdf.utils import get_plain_text
|
||||
|
||||
|
||||
EXTRA_CONTEXT = {
|
||||
|
@ -12,6 +13,7 @@ def get_context(config, content):
|
|||
context = config['context'].copy()
|
||||
context['title'] = config['title']
|
||||
context = dict(context, **EXTRA_CONTEXT, **{
|
||||
'word_count': word_count(content)
|
||||
})
|
||||
if config.get('show_word_count'):
|
||||
context['word_count'] = word_count(get_plain_text(content))
|
||||
return context
|
||||
|
|
|
@ -76,6 +76,13 @@ def validate_toc(config):
|
|||
raise ConfigValidationException("Table of contents key should be either true or false")
|
||||
|
||||
|
||||
def validate_wordcount(config):
|
||||
if 'show_word_count' not in config:
|
||||
return
|
||||
if type(config['show_word_count']) != bool:
|
||||
raise ConfigValidationException("Show word count key should be either true or false")
|
||||
|
||||
|
||||
def validate_config(config):
|
||||
logger.debug("Validating Config...")
|
||||
for validator in [
|
||||
|
@ -84,7 +91,8 @@ def validate_config(config):
|
|||
test_output,
|
||||
validate_bibliography,
|
||||
validate_context,
|
||||
validate_toc
|
||||
validate_toc,
|
||||
validate_wordcount
|
||||
]:
|
||||
validator(config)
|
||||
logger.debug("Config Ok!")
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import shutil
|
||||
import os
|
||||
import logging
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
logger = logging.getLogger(__file__)
|
||||
|
||||
|
@ -19,3 +20,14 @@ def safe_list_get(l, idx, default):
|
|||
return l[idx]
|
||||
except IndexError:
|
||||
return default
|
||||
|
||||
|
||||
def get_plain_text(content):
|
||||
soup = BeautifulSoup(content, 'html.parser')
|
||||
body = soup.find('body')
|
||||
try:
|
||||
body.find('h1', class_='references-title').extract()
|
||||
body.find('div', class_='references').extract()
|
||||
except AttributeError:
|
||||
pass
|
||||
return body.text
|
||||
|
|
|
@ -13,3 +13,4 @@ context:
|
|||
turnitin_number: 789123
|
||||
title: test title
|
||||
toc: true
|
||||
show_word_count: true
|
||||
|
|
Reference in a new issue