# Makefile for generating "manual.txt" and "manual.pdf" from "manual.pmd" and "zint.1" from "zint.1.pmd" using pandoc
# Copyright (C) 2022-2024 <rstuart114@gmail.com>
# vim: set ts=4 sw=4 noet :
#
# Requires a recent version of pandoc, plus pandoc-tablenos, xelatex and various other packages - see "README"
# .svg images generated by "zint_images.sh"

SOURCE = manual.pmd
OUT_PDF = manual.pdf
OUT_TXT = manual.txt
INC_HEADER_PDF = inc_header_pdf.tex
INC_BEFORE_BODY_PDF = inc_before_body_pdf.tex
INC_PDF = --include-in-header $(INC_HEADER_PDF) --include-before-body $(INC_BEFORE_BODY_PDF)
INCLUDES_TXT = inc_header_txt.tex
INC_TXT = --include-in-header $(INCLUDES_TXT)
SOURCE_MAN_PAGE = zint.1.pmd
OUT_MAN_PAGE = zint.1
IMAGES = \
	images/zint.png \
	images/zint-qt.png \
	images/gui_main.png \
	images/gui_delete.png \
	images/gui_zap.png \
	images/gui_menus.png \
	images/gui_composite.png \
	images/gui_segs.png \
	images/gui_c25inter.png \
	images/gui_aztec.png \
	images/gui_appearance.png \
	images/gui_scaling.png \
	images/gui_set_printing_scale.png \
	images/gui_black_eye.png \
	images/gui_white_eye.png \
	images/gui_swap.png \
	images/gui_colour.png \
	images/gui_data_dialog.png \
	images/gui_sequence.png \
	images/gui_export.png \
	images/gui_cli_equivalent.png \
	images/tcl_demo.png \
	images/pdf417_heightperrow.svg \
	images/code128_box.svg \
	images/qrcode_box.svg \
	images/code128_green.svg \
	images/code128_green_alpha.svg \
	images/code128_rotate90.svg \
	images/datamatrix_euro.svg \
	images/datamatrix_big5.svg \
	images/qrcode_binary_utf8.svg \
	images/codeone_s_dotty.svg \
	images/aztec_segs.svg \
	images/datamatrix_structapp.svg \
	images/code128_small_bold.svg \
	images/code11.svg \
	images/c25standard.svg \
	images/c25iata.svg \
	images/c25ind.svg \
	images/c25inter.svg \
	images/c25logic.svg \
	images/itf14.svg \
	images/itf14_border0.svg \
	images/dpleit.svg \
	images/dpident.svg \
	images/upca.svg \
	images/upca_5.svg \
	images/upce.svg \
	images/eanx13.svg \
	images/eanx5.svg \
	images/eanx8_5.svg \
	images/isbnx.svg \
	images/plessey.svg \
	images/msi_plessey.svg \
	images/telepen.svg \
	images/telepen_num.svg \
	images/code39.svg \
	images/excode39.svg \
	images/code93.svg \
	images/pzn.svg \
	images/logmars.svg \
	images/code32.svg \
	images/hibc_39.svg \
	images/vin.svg \
	images/codabar.svg \
	images/pharma.svg \
	images/code128.svg \
	images/code128ab.svg \
	images/gs1_128.svg \
	images/ean14.svg \
	images/nve18.svg \
	images/hibc_128.svg \
	images/dpd.svg \
	images/upu_s10.svg \
	images/dbar_omn.svg \
	images/dbar_truncated.svg \
	images/dbar_ltd.svg \
	images/dbar_exp.svg \
	images/koreapost.svg \
	images/channel.svg \
	images/bc412.svg \
	images/code128_stacked.svg \
	images/code128_stacked_sep2.svg \
	images/codablockf.svg \
	images/code16k.svg \
	images/pdf417.svg \
	images/pdf417comp.svg \
	images/micropdf417.svg \
	images/dbar_stk.svg \
	images/dbar_omnstk.svg \
	images/dbar_expstk.svg \
	images/code49.svg \
	images/eanx_cc_a.svg \
	images/eanx_cc_b.svg \
	images/gs1_128_cc_c.svg \
	images/pharma_two.svg \
	images/postnet.svg \
	images/planet.svg \
	images/auspost.svg \
	images/ausroute.svg \
	images/ausreply.svg \
	images/ausredirect.svg \
	images/kix.svg \
	images/rm4scc.svg \
	images/mailmark_4s.svg \
	images/usps_imail.svg \
	images/japanpost.svg \
	images/hibc_dm.svg \
	images/mailmark_2d.svg \
	images/qrcode.svg \
	images/microqr.svg \
	images/rmqr.svg \
	images/upnqr.svg \
	images/maxicode.svg \
	images/aztec.svg \
	images/azrune.svg \
	images/codeone.svg \
	images/gridmatrix.svg \
	images/dotcode.svg \
	images/hanxin.svg \
	images/ultra.svg \
	images/fim.svg \
	images/flat.svg \
	images/daft_rm4scc.svg
MAIN_FONT = mainfont="TeX Gyre Pagella"
MONO_FONT = monofont="Liberation Mono"
CJK_FONT = CJKmainfont="WenQuanYi Micro Hei Mono"
PDF_OPTS = --pdf-engine=xelatex --filter pandoc-tablenos -M tablenos-warning-level=0 \
			--highlight-style=haddock -V colorlinks -V geometry:margin=20mm -V papersize=a4 -V csquotes=true --dpi=300
TEX_MAN_PAGE = zint.1.tex
TXT_OPTS = --filter pandoc-tablenos -M tablenos-warning-level=0 --columns 80 --eol=lf -t plain
MAN_PAGE_OPTS = -s -t man

all : $(OUT_PDF) $(OUT_TXT) $(OUT_MAN_PAGE)

$(OUT_PDF) : $(SOURCE) $(SOURCE_MAN_PAGE) $(INC_HEADER_PDF) $(INC_BEFORE_BODY_PDF) $(IMAGES) Makefile
	pandoc $(SOURCE_MAN_PAGE) -f markdown \
			$(PDF_OPTS) \
			-o $(TEX_MAN_PAGE)
	sed -i -e 's/section{/subsection{/' $(TEX_MAN_PAGE) # Convert man page sections to subsections
	pandoc $(SOURCE) -f markdown $(INC_PDF) --toc --toc-depth=4 \
			-V $(MAIN_FONT) -V $(MONO_FONT) -V $(CJK_FONT) -V block-headings \
			$(PDF_OPTS) \
			--include-after-body $(TEX_MAN_PAGE) \
			-o $(OUT_PDF)

$(OUT_TXT) : $(SOURCE) $(SOURCE_MAN_PAGE) $(INCLUDES_TXT) Makefile
	pandoc $(SOURCE) $(SOURCE_MAN_PAGE) -f markdown $(INC_TXT) --toc --toc-depth=4 \
			-V $(MAIN_FONT) -V $(MONO_FONT) -V $(CJK_FONT) \
			$(TXT_OPTS) \
			-o $(OUT_TXT)
	# Indent Man Page sections in TOC, remove trailing spaces, echoed image tags, tbl: hashes
	# & clean up Table captions
	sed -i \
		-e 's/^\(-   [A-Z][A-Z ]*\)$$/    \1/' \
		-e 's/  *$$//' \
		-e '/^\[.*\]$$/{N;N;s/\[\(.*\)\]\n\n\1/[\1]/;p;d}' \
		-e 's/ *{#tbl:[^}]*}//' \
		-e 's/: Table\xC2\xA0: \([^:]*\):/Table : \1/' \
		$(OUT_TXT)
	# Wrap
	sed -i '/.\{81\}/{s/.\{80\}/&\n/}' $(OUT_TXT)

$(OUT_MAN_PAGE) : $(SOURCE_MAN_PAGE) Makefile
	pandoc $(SOURCE_MAN_PAGE) -f markdown \
			$(MAN_PAGE_OPTS) \
			-o $(OUT_MAN_PAGE)

# For debugging

manual.tex : $(SOURCE) $(SOURCE_MAN_PAGE) $(INC_HEADER_PDF) $(INC_BEFORE_BODY_PDF) $(IMAGES) Makefile
	pandoc $(SOURCE_MAN_PAGE) -f markdown \
			$(PDF_OPTS) \
			-o $(TEX_MAN_PAGE)
	sed -i -e 's/section{/subsection{/' $(TEX_MAN_PAGE) # Convert man page sections to subsections
	pandoc $(SOURCE) -f markdown $(INC_PDF) --toc --toc-depth=4 \
			-V $(MAIN_FONT) -V $(MONO_FONT) -V $(CJK_FONT) -V block-headings \
			$(PDF_OPTS) \
			--include-after-body $(TEX_MAN_PAGE) \
			-s -o manual.tex

# HTML one-page (uses modified "templates/styles.html", unchanged "templates/default.html")

OUT_HTML = manual.html
HTML_OPTS = --filter pandoc-tablenos -M tablenos-warning-level=0 --highlight-style=haddock \
			--template=templates/default.html --eol=lf -s -t html
INC_BEFORE_BODY_HTML = inc_before_body_html.html
INC_HTML = --include-before-body $(INC_BEFORE_BODY_HTML)
TEMPLATES_HTML = templates/default.html templates/styles.html

$(OUT_HTML) : $(SOURCE) $(SOURCE_MAN_PAGE) $(INC_BEFORE_BODY_HTML) $(TEMPLATES_HTML) Makefile
	pandoc $(SOURCE) $(SOURCE_MAN_PAGE) -f markdown+link_attributes $(INC_HTML) --toc --toc-depth=4 \
			-V $(MAIN_FONT) -V $(MONO_FONT) -V $(CJK_FONT) \
			$(HTML_OPTS) \
			-o $(OUT_HTML)
	# Indent Man Page sections in TOC, change Man Page sections h1 -> h2, remove Man Page meta, fix Table captions
	sed -i \
		-e 's/\(Man Page ZINT(1)<\/a>\)<\/li>/\1<ul>/' \
		-e 's/\(AUTHORS<\/a><\/li>\)/\1<\/ul><\/li>/' \
		-e 's/^<h1\([^>]*>[A-Z][A-Z ]*<\/\)h1>$$/<h2\1h2>/' \
		-e 's/^<p>% ZINT(1)[^<]*<\/p>//' \
		-e 's/<caption><span>Table\(.:[^:]*\):/<caption><span>Table\1/' \
		$(OUT_HTML)
