summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe Carstairs <me@joeac.net>2026-06-30 12:34:56 +0100
committerJoe Carstairs <me@joeac.net>2026-06-30 12:36:19 +0100
commit3513c8d066dac595f92ed12dd37da9cbd51cc186 (patch)
treea7816e1008c4c2fe4e8cb9a4411f7e31d061e6c7
parent39f62e8405e2585ef87a69cad0a4f246d4cac880 (diff)
uses pandoc wordcount lua-filter and no-refs build via latex for more
accurate word-counts
-rw-r--r--.gitignore2
-rw-r--r--Makefile51
-rw-r--r--filters/wordcount.lua28
3 files changed, 69 insertions, 12 deletions
diff --git a/.gitignore b/.gitignore
index 158f858..94e96c1 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,5 @@
*.pdf
.vim
dissertation.md
+dissertation.latex
+dissertation-no-references.latex
diff --git a/Makefile b/Makefile
index 45f0260..0b6c869 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,7 @@
BIBLIOGRAPHY := bibliography.bib
TEMPLATE_FILES := $(wildcard templates/*)
TEMPLATE := templates/default.latex
+WORDCOUNT_FILTER := filters/wordcount.lua
DISSERTATION_FRONTMATTER := frontmatter.yml
DISSERTATION_MARKDOWN := dissertation.md
INTRODUCTION_MARKDOWN := chapter0.md
@@ -11,7 +12,10 @@ CHAPTER_4_MARKDOWN := chapter4.md
CONCLUSION_MARKDOWN := chapter5.md
LOG_MARKDOWN := log.md
SCRAPBOOK_MARKDOWN := scrapbook.md
+DISSERTATION_LATEX := dissertation.latex
+DISSERTATION_LATEX_NO_REFERENCES := dissertation-no-references.latex
DISSERTATION_PDF := dissertation.pdf
+DISSERTATION_PDF_NO_REFERENCES := dissertation-no-references.pdf
LOG_PDF := log.pdf
SCRAPBOOK_PDF := scrapbook.pdf
@@ -58,15 +62,23 @@ $(SCRAPBOOK_PDF): $(SCRAPBOOK_MARKDOWN) $(BIBLIOGRAPHY) $(TEMPLATE_FILES)
dissertation: $(DISSERTATION_PDF)
-$(DISSERTATION_PDF): $(DISSERTATION_MARKDOWN) $(BIBLIOGRAPHY) $(TEMPLATE_FILES)
+$(DISSERTATION_PDF): $(DISSERTATION_LATEX)
pandoc \
--standalone \
- --from markdown \
+ --from latex \
--to pdf \
--pdf-engine pdflatex \
+ --output $(DISSERTATION_PDF) \
+ $(DISSERTATION_LATEX)
+
+$(DISSERTATION_LATEX): $(DISSERTATION_MARKDOWN) $(BIBLIOGRAPHY) $(TEMPLATE_FILES)
+ pandoc \
+ --standalone \
+ --from markdown \
+ --to latex \
--citeproc \
--template $(TEMPLATE) \
- --output $(DISSERTATION_PDF) \
+ --output $(DISSERTATION_LATEX) \
$(DISSERTATION_MARKDOWN)
.SILENT: $(DISSERTATION_MARKDOWN)
@@ -91,16 +103,31 @@ $(DISSERTATION_MARKDOWN): $(DISSERTATION_FRONTMATTER) $(INTRODUCTION_MARKDOWN) $
echo "" >> $(DISSERTATION_MARKDOWN)
.SILENT: wordcount
-wordcount: $(DISSERTATION_MARKDOWN)
- echo "Introduction: ~$$(cat $(INTRODUCTION_MARKDOWN) | wc -w)"
- echo "Chapter 1: ~$$(cat $(CHAPTER_1_MARKDOWN) | wc -w)"
- echo "Chapter 2: ~$$(cat $(CHAPTER_2_MARKDOWN) | wc -w)"
- echo "Chapter 3: ~$$(cat $(CHAPTER_3_MARKDOWN) | wc -w)"
- echo "Chapter 4: ~$$(cat $(CHAPTER_4_MARKDOWN) | wc -w)"
- echo "Conclusion: ~$$(cat $(CONCLUSION_MARKDOWN) | wc -w)"
+wordcount: $(DISSERTATION_PDF_NO_REFERENCES)
+ echo "Introduction: $$(pandoc --lua-filter $(WORDCOUNT_FILTER) $(INTRODUCTION_MARKDOWN))"
+ echo "Chapter 1: $$(pandoc --lua-filter $(WORDCOUNT_FILTER) $(CHAPTER_1_MARKDOWN))"
+ echo "Chapter 2: $$(pandoc --lua-filter $(WORDCOUNT_FILTER) $(CHAPTER_2_MARKDOWN))"
+ echo "Chapter 3: $$(pandoc --lua-filter $(WORDCOUNT_FILTER) $(CHAPTER_3_MARKDOWN))"
+ echo "Chapter 4: $$(pandoc --lua-filter $(WORDCOUNT_FILTER) $(CHAPTER_4_MARKDOWN))"
+ echo "Conclusion: $$(pandoc --lua-filter $(WORDCOUNT_FILTER) $(CONCLUSION_MARKDOWN))"
echo "Footnotes: ???"
echo "References: ???"
- echo "Total: ~$$(cat $(DISSERTATION_MARKDOWN) | tail -n +6 | wc -w) + footnotes"
+ echo "Total: $$(pdfgrep -o '\w+' $(DISSERTATION_PDF_NO_REFERENCES) | wc -w) ($$(pandoc --lua-filter $(WORDCOUNT_FILTER) $(DISSERTATION_MARKDOWN)) + footnotes)"
+
+$(DISSERTATION_PDF_NO_REFERENCES): $(DISSERTATION_LATEX_NO_REFERENCES)
+ pandoc \
+ --standalone \
+ --from latex \
+ --to pdf \
+ --pdf-engine pdflatex \
+ --output $(DISSERTATION_PDF_NO_REFERENCES) \
+ $(DISSERTATION_LATEX_NO_REFERENCES)
+
+bib_start_line_index = $(shell grep -nm 1 "^\\\\begin{CSLReferences}" $(DISSERTATION_LATEX) | sed "s/^\([0-9]*\):.*$$/\1/")
+bib_end_line_index = $(shell grep -nm 1 "^\\\\end{CSLReferences}" $(DISSERTATION_LATEX) | sed "s/^\([0-9]*\):.*$$/\1/")
+$(DISSERTATION_LATEX_NO_REFERENCES): $(DISSERTATION_LATEX)
+ head -n $$(( $(bib_start_line_index) - 1 )) $(DISSERTATION_LATEX) > $(DISSERTATION_LATEX_NO_REFERENCES)
+ tail -n +$$(( $(bib_end_line_index) + 1 )) $(DISSERTATION_LATEX) >> $(DISSERTATION_LATEX_NO_REFERENCES)
clean:
- rm -f $(LOG_PDF) $(SCRAPBOOK_PDF) $(DISSERTATION_MARKDOWN) $(DISSERTATION_PDF)
+ rm -f $(LOG_PDF) $(SCRAPBOOK_PDF) $(DISSERTATION_MARKDOWN) $(DISSERTATION_LATEX) $(DISSERTATION_LATEX_NO_REFERENCES) $(DISSERTATION_PDF) $(DISSERTATION_PDF_NO_REFERENCES)
diff --git a/filters/wordcount.lua b/filters/wordcount.lua
new file mode 100644
index 0000000..587f45d
--- /dev/null
+++ b/filters/wordcount.lua
@@ -0,0 +1,28 @@
+-- counts words in a document
+
+words = 0
+
+wordcount = {
+ Str = function(el)
+ -- we don't count a word if it's entirely punctuation:
+ if el.text:match("%P") then
+ words = words + 1
+ end
+ end,
+
+ Code = function(el)
+ _,n = el.text:gsub("%S+","")
+ words = words + n
+ end,
+
+ CodeBlock = function(el)
+ _,n = el.text:gsub("%S+","")
+ words = words + n
+ end
+}
+
+function Pandoc(el)
+ pandoc.walk_block(pandoc.Div(el.blocks), wordcount)
+ print(words)
+ os.exit(0)
+end