From b8f4c28be1d5dabab871eb60ec259baa926914e6 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Wed, 28 Mar 2018 19:47:06 +0100 Subject: [PATCH] Inject auto-calculated wordcount --- build.py | 35 ++++++++++++++++++++++++++++------- header-footer.tex | 2 +- main.tex | 4 ++-- wordcount.yaml | 3 +++ 4 files changed, 34 insertions(+), 10 deletions(-) create mode 100644 wordcount.yaml diff --git a/build.py b/build.py index f15c432..df65316 100755 --- a/build.py +++ b/build.py @@ -3,6 +3,8 @@ import argparse import pathlib import os import subprocess +import re +import tempfile CWD = pathlib.Path(os.getcwd()) @@ -10,15 +12,18 @@ BASE_DIR = pathlib.Path(os.path.dirname(os.path.abspath(__file__))) TEMPLATE_FILE = BASE_DIR / 'main.tex' OUTPUT_FILE = CWD / 'output.pdf' CONTEXT_FILE = CWD / 'context.yaml' -BUILD_PASSES = 2 +WORDCOUNT_TEMPLATE = BASE_DIR / 'wordcount.yaml' -def build(input_file: pathlib.Path): +def build(input_file: pathlib.Path, additional_args=None): build_args = ['pandoc', str(input_file)] if CONTEXT_FILE.exists(): build_args.append(str(CONTEXT_FILE)) + if additional_args is not None: + build_args.extend(additional_args) + build_args.extend([ '--template', str(TEMPLATE_FILE), @@ -28,12 +33,28 @@ def build(input_file: pathlib.Path): return subprocess.run(build_args, cwd=str(BASE_DIR), check=True) -def parse_args(args=None): +def get_word_count() -> int: + words = subprocess.check_output([ + 'pdftotext', str(OUTPUT_FILE), '-' + ], cwd=str(BASE_DIR)) + return len(re.findall(r'\w+', words.decode())) + + +def write_wordcount() -> pathlib.Path: + _, out_file_path = tempfile.mkstemp(suffix='.yaml') + wordcount = get_word_count() + with open(out_file_path, 'w') as out_file: + with WORDCOUNT_TEMPLATE.open() as template: + out_file.write(template.read().format(wordcount=wordcount)) + return pathlib.Path(out_file_path) + + +def parse_args(): parser = argparse.ArgumentParser() parser.add_argument("-v", "--verbose", help="Set verbosity level (repeat argument)", action="count", default=0) parser.add_argument("input", help="Input File", action='store', type=pathlib.Path,) parser.add_help = True - return parser.parse_args(args=args) + return parser.parse_args() if __name__ == '__main__': @@ -41,6 +62,6 @@ if __name__ == '__main__': input_file = args.input.resolve() if not input_file.is_file(): raise ValueError("Failed to find {}".format(input_file)) - for i in range(2): - print("Building pass {}.".format(i + 1)) - build(input_file) + build(input_file) + wordcount_metadata_file = write_wordcount() + build(input_file, [str(wordcount_metadata_file)]) diff --git a/header-footer.tex b/header-footer.tex index be3e4f2..5a9cdab 100644 --- a/header-footer.tex +++ b/header-footer.tex @@ -1,6 +1,6 @@ \pagestyle{fancy} -\rfoot{$wordcount$ words} +\rfoot{\wordcount words} \cfoot{\thepage\ of \pageref{LastPage}} \lhead{\thetitle} \rhead{\date} diff --git a/main.tex b/main.tex index ff5673f..b9196f4 100644 --- a/main.tex +++ b/main.tex @@ -1,6 +1,5 @@ \documentclass[12pt,titlepage,a4paper,twoside]{article} \include{packages} - \include{header-footer} \renewcommand{\thesection}{} \renewcommand{\thesubsection}{\arabic{section}.\arabic{subsection}} @@ -13,10 +12,11 @@ \title{$title$} \newcommand{\subtitle}{$subtitle$} + \newcommand{\wordcount}{$wordcount$} \author{Some Author} \date{2018-01-01} - + \include{header-footer} \begin{document} diff --git a/wordcount.yaml b/wordcount.yaml new file mode 100644 index 0000000..e8b409b --- /dev/null +++ b/wordcount.yaml @@ -0,0 +1,3 @@ +--- +wordcount: {wordcount} +...