Skip to content

Commit

Permalink
Render ePub and avoid code overflow in PDF
Browse files Browse the repository at this point in the history
  • Loading branch information
TomBener committed May 28, 2024
1 parent 6bda232 commit b794988
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 33 deletions.
26 changes: 18 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
# `make citebib`: Extract all bibliographies cited as BibLaTeX
# `make` or `make all`: Render DOCX, HTML, PDF and Reveal.js slides at once
# `make print`: Render PDF for print
# `make watermark`: Render PDF with watermark
# `make clean`: Remove auxiliary and output files
# `make` or `make all`: Render DOCX, HTML, PDF, ePub and Reveal.js slides at once.
# `make docx`: Render DOCX.
# `make html`: Render HTML.
# `make pdf`: Render PDF.
# `make epub`: Render ePub.
# `make slides`: Render Reveal.js slides.
# `make print`: Render PDF for print.
# `make watermark`: Render PDF with watermark.
# `make citebib`: Extract all bibliographies cited as BibLaTeX file `citebib.bib`.
# `make clean`: Remove auxiliary and output files.

# Render DOCX, HTML, PDF and Reveal.js slides
# Render DOCX, HTML, PDF, ePub and Reveal.js slides at once
.PHONY: all
all: docx html pdf slides
all: docx html pdf epub slides

# Pandoc command for extracting bibliographies
CITE := @pandoc \
Expand Down Expand Up @@ -49,7 +54,7 @@ PDF_OUTPUT := index.pdf

# Special handling for print PDF
.PHONY: print
print: PDF_OPTION := -V draft -M biblatexoptions="backend=biber,backref=false,dashed=false,\
print: PDF_OPTION := -V draft -M biblatexoptions="backend=biber,backref=false,dashed=false, \
gblabelref=false,gblanorder=englishahead,gbnamefmt=lowercase,gbfieldtype=true,doi=false"
print: PDF_OUTPUT := print.pdf
print: pdf
Expand All @@ -60,6 +65,11 @@ watermark: PDF_OPTION := -V watermark=true
watermark: PDF_OUTPUT := watermark.pdf
watermark: pdf

# Render ePub
epub: dependencies
$(QUARTO) $@ -L _extensions/localize-cnbib.lua -L _extensions/bib-quotes.lua \
--filter _extensions/sort-cnbib.py --filter _extensions/auto-correct.py

# Render Reveal.js slides
slides: dependencies
@quarto render slides.qmd --to revealjs --filter _extensions/auto-correct.py
Expand Down
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

This repository provides a comprehensive guide and toolset for writing academic
papers in Chinese, such as the localization and sorting of Chinese
bibliographies, conversion of quotes, and adding space between Chinese and
English Characters. With the help of these templates and scripts, you can write
your academic papers in Markdown, and convert them into various formats like
Word, HTML, LaTeX, PDF, and EPUB via Quarto.
bibliographies, conversion of Chinese quotes, and correcting spaces between
Chinese and English characters. With the help of these templates and scripts,
you can write your academic papers in Markdown, and convert them into various
formats like Word, HTML, LaTeX, PDF, and ePub via Quarto with ease.

## Prerequisites

- [Quarto](https://quarto.org), with [Pandoc](https://pandoc.org) included, and you can install TinyTeX via `quarto install tinytex --update-path`.
- [Quarto](https://quarto.org), with [Pandoc](https://pandoc.org) included, and
you can install TinyTeX via `quarto install tinytex --update-path`.
- Python, and the following packages:
- [autocorrect_py](https://github.com/huacnlee/autocorrect/tree/main/autocorrect-py)
- [pypinyin](https://github.com/mozillazg/python-pinyin)
Expand All @@ -25,14 +26,15 @@ Word, HTML, LaTeX, PDF, and EPUB via Quarto.

This project uses a Makefile to manage the build process. Here are the available commands:

- `make citebib`: Extract all bibliographies cited as BibLaTeX file `citebib.bib`.
- `make` or `make all`: Render DOCX, HTML, PDF, ePub and Reveal.js slides at once.
- `make docx`: Render DOCX.
- `make html`: Render HTML.
- `make pdf`: Render PDF.
- `make epub`: Render ePub.
- `make slides`: Render Reveal.js slides.
- `make` or `make all`: Render DOCX, HTML, PDF and Reveal.js slides at once.
- `make print`: Render PDF for print.
- `make watermark`: Render PDF with watermark.
- `make citebib`: Extract all bibliographies cited as BibLaTeX file `citebib.bib`.
- `make clean`: Remove auxiliary and output files.

## License
Expand Down
4 changes: 2 additions & 2 deletions _extensions/bib-quotes.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- Process quotes for Chinese bibliographies in docx and html output
-- Process quotes for Chinese bibliographies in docx, html and epub outputs

--- Copyright: © 2024 Tom Ben
--- License: MIT License
Expand All @@ -20,7 +20,7 @@ function quotes_in_bib(block)
string.format(
'<w:r><w:rPr><w:rFonts w:hint="eastAsia"/></w:rPr><w:t xml:space="preserve">%s</w:t></w:r>',
el.text))
elseif FORMAT:match 'html' then
elseif FORMAT:match 'html' or FORMAT:match 'epub' then
local replaced_text = el.text
if el.text == "" then
replaced_text = ""
Expand Down
4 changes: 4 additions & 0 deletions _quarto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ format:
# - gbalign=gb7714-2015 # Add numeric label, redundant for author-year style
# - autocite=plain # Add bracket squares in citiations for numeric style

epub:
epub-cover-image: figures/cover.png
rights: © 2024 Tom Ben

revealjs:
template: _styles/revealjs.html
theme: _styles/metropolis.scss
Expand Down
10 changes: 10 additions & 0 deletions _styles/custom.tex
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,13 @@
\newunicodechar{‹}{‘}
\newunicodechar{›}{’}

% Avoid overflow of the code block
\usepackage{fvextra}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{breaklines,commandchars=\\\{\}}
\RecustomVerbatimEnvironment{verbatim}{Verbatim}{
showspaces = false,
showtabs = false,
breaksymbolleft={},
breaklines
}

2 changes: 1 addition & 1 deletion contents/1-introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Word 却并不是最适合学术论文写作的工具。
[Pandoc](https://pandoc.org)
[@macfarlane2024]
的加持下,Markdown 可以输出多种格式的文档,
例如 Word、HTML、LaTeX、PDF、EPUB 等。
例如 Word、HTML、LaTeX、PDF、ePub 等。
基于 Pandoc 开发的 [Quarto](https://quarto.org)
[@allaire2024]
不仅能够直接在论文中运行 Python、R 等代码,
Expand Down
34 changes: 21 additions & 13 deletions contents/3-sort-cnbib.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,27 @@
根据拼音对中文文献进行排序,并且对姓氏读音的多音字进行了特殊处理:

```py
# 多音字的姓氏拼音
surname_map = {
'': 'ge3',
'': 'kan4',
'': 'ren2',
'': 'shan4',
'': 'xie4',
'': 'yan1',
'': 'yu4',
'': 'yue4',
'': 'zeng1',
'': 'zha1',
}
def special_pinyin(text):
# 多音字的姓氏拼音
surname_map = {
'': 'ge3',
'': 'kan4',
'': 'ren2',
'': 'shan4',
'': 'xie4',
'': 'yan1',
'': 'yu4',
'': 'yue4',
'': 'zeng1',
'': 'zha1',
}

if contains_chinese(text):
name = text.split(",")[0] if "," in text else text
surname = name[0]
return surname_map.get(surname, "".join([i[0] for i in pinyin(name, style=Style.TONE3)]))
else:
return None
```

对于参考文献中中文和英文文献哪个在前的问题,
Expand Down
4 changes: 2 additions & 2 deletions contents/5-pangu-space.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@

通过 `auto-correct.py` filter,可以实现修改 Pandoc AST,
在 Pandoc 转换过程中自动纠正中英文、数字之间的空格问题,
无需在得到目标格式后再进行修改,特别是对于某些格式(如 EPUB)来说,
无需在得到目标格式后再进行修改,特别是对于某些格式(如 ePub)来说,
无法在转换完成后再进行修改,因此这个 filter 的优势就体现出来了。

需要注意的是,LaTeX 和 Word 会自动处理中英文之间的空格,
因此无需使用这个 Python filter,只有在转换为其他格式时才需要使用,
例如 HTML、EPUB 等格式。
例如 HTML、ePub 等格式。
Binary file added figures/cover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b794988

Please sign in to comment.