Skip to content

Commit aef5fae

Browse files
pandoc tool
Signed-off-by: Laurent Martin <laurent.martin.l@gmail.com>
1 parent 4359252 commit aef5fae

4 files changed

Lines changed: 101 additions & 53 deletions

File tree

build/doc/pandoc/README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,22 @@ clean:
2222

2323
There is a default target for `%.pdf` from `%.md`.
2424

25-
If the source and destination have different basenames or path, then it is possible to do:
25+
If the source and destination have different base names or path, then it is possible to do:
2626

2727
```makefile
2828
$(eval $(call markdown_to_pdf,source.md,target.pdf))
2929
```
3030

3131
1. Run `make` to generate the PDF file.
3232

33-
The markdown file can include a section like this with `pandoc` metadata:
33+
The markdown file can include a section like this with `pandoc` defaults:
3434

3535
```xml
3636
<!--
37-
PANDOC_META_BEGIN
38-
subtitle: "subtitle here"
39-
author: "Johnny Beegood"
40-
PANDOC_META_END
37+
PANDOC_DEFAULTS_BEGIN
38+
metadata:
39+
subtitle: "subtitle here"
40+
author: "Johnny Beegood"
41+
PANDOC_DEFAULTS_END
4142
-->
4243
```

build/doc/pandoc/pandoc.mak

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,16 @@
11
# See README.md for more information
22
DIR_PANDOC := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
3-
DEF_COMMON=$(DIR_PANDOC)defaults_common.yaml
4-
DEF_PDF=$(DIR_PANDOC)defaults_pdf.yaml
5-
DEF_HTML=$(DIR_PANDOC)defaults_html.yaml
6-
PANDOC_DEPS=\
7-
$(DIR_PANDOC)break_replace.lua \
8-
$(DEF_COMMON) \
9-
$(DEF_PDF) \
10-
$(DEF_HTML) \
11-
$(DIR_PANDOC)find_admonition.lua \
12-
$(DIR_PANDOC)gfm_admonition.css \
13-
$(DIR_PANDOC)gfm_admonition.lua \
14-
$(DIR_PANDOC)pdf_after_body.tex \
15-
$(DIR_PANDOC)pdf_in_header.tex \
16-
$(DIR_PANDOC)pandoc.mak
17-
TEX_ADD_SUFFIX=.pandoc_add.tex
18-
META_ADD_SUFFIX=.pandoc_meta.yaml
3+
DOC_GENERATOR := $(DIR_PANDOC)../../lib/pandoc.rb
4+
PANDOC_DEPS := $(shell ruby $(DOC_GENERATOR) deps) $(DIR_PANDOC)pandoc.mak $(DOC_GENERATOR)
195

206
define markdown_to_pdf
217
$(2): $(1) $$(PANDOC_DEPS)
22-
-sed -n '/PANDOC_META_BEGIN/,/PANDOC_META_END/p' $$< | grep -v PANDOC_META > $$<$(META_ADD_SUFFIX)
23-
echo '\\graphicspath{{$(DIR_PANDOC)}}' > $$<$(TEX_ADD_SUFFIX)
24-
set -x &&\
25-
if git status --porcelain $$< > /dev/null 2>&1 && test -z "$$$$(git status --porcelain $$<)";then \
26-
ref="-r $$$$(git log -1 --pretty="format:%cd" --date=unix $$<)";fi &&\
27-
pandoc \
28-
--include-in-header=$$<$(TEX_ADD_SUFFIX) \
29-
--defaults=$$(DEF_COMMON) \
30-
--defaults=$$(DEF_PDF) \
31-
--variable=date:"$$$$(/bin/date $$$$ref '+%Y/%m/%d')" \
32-
--metadata-file=$$<$(META_ADD_SUFFIX) \
33-
--output=$$@ \
34-
$$<
35-
rm -f $$<$(META_ADD_SUFFIX) $$<$(TEX_ADD_SUFFIX)
8+
$(DOC_GENERATOR) pdf $$< $$@
369
endef
3710

3811
define markdown_to_html
3912
$(2): $(1) $$(PANDOC_DEPS)
40-
pandoc \
41-
--defaults=$$(DEF_COMMON) \
42-
--defaults=$$(DEF_HTML) \
43-
--output=$$@ \
44-
$$<
13+
$(DOC_GENERATOR) html $$< $$@
4514
endef
4615

4716
$(eval $(call markdown_to_pdf,%.md,%.pdf))

build/lib/doc_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ def include_asession
303303
['$(PATH_FILE_LIST)', 'file_list.txt'],
304304
['$(PATH_VAULT_FILE)', '/secure/vault_file'],
305305
['$(path_file_pair_list)', 'file_pair_list.txt'],
306+
['$(remote_host)', 'app.example.com'],
306307
['"my_password"', '"my_password_here"'],
307308
['$(name) $(PACKAGE_TITLE_BASE)', 'package title'],
308309
[/^--base=.*/, '--base=test'],

build/lib/pandoc.rb

Lines changed: 89 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
#!/usr/bin/env ruby
12
# frozen_string_literal: true
23

34
require 'pathname'
45
require_relative 'paths'
56
require_relative 'build_tools'
67

78
include BuildTools
9+
include Paths
810

911
# Path to pandoc root directory
1012
PATH_PANDOC_ROOT = ENV.key?('DIR_PANDOC') ? Pathname.new(ENV['DIR_PANDOC']) : Paths::PANDOC
@@ -21,21 +23,21 @@
2123
'pdf_in_header.tex'
2224
].map{ |f| (PATH_PANDOC_ROOT / f).to_s}.freeze
2325

24-
# Extract pandoc metadata from markdown comment
25-
def extract_metadata_file(md)
26-
metadata_file = TMP / 'pandoc_meta.yaml'
26+
# Extract pandoc defaults from markdown comment
27+
def extract_pandoc_defaults_file(md)
28+
custom_tmp_file = TMP / 'custom_defaults.yaml'
2729
inside = false
28-
File.open(metadata_file, 'w') do |out|
30+
File.open(custom_tmp_file, 'w') do |out|
2931
File.foreach(md.to_s) do |line|
3032
if !inside
31-
inside = true if line.include?('PANDOC_META_BEGIN')
33+
inside = true if line.include?('PANDOC_DEFAULTS_BEGIN')
3234
next
3335
end
34-
break if line.include?('PANDOC_META_END')
36+
break if line.include?('PANDOC_DEFAULTS_END')
3537
out.write(line)
3638
end
3739
end
38-
metadata_file
40+
custom_tmp_file
3941
end
4042

4143
# Get latest git change date, or else just the file's modification date
@@ -54,7 +56,7 @@ def get_change_date(md)
5456
def generate_gfx_paths_latex(paths)
5557
result = TMP / 'pandoc_add.tex'
5658
# https://latexref.xyz/_005cgraphicspath.html
57-
result.write("\\graphicspath{#{paths.map{ |p| "{#{p}}"}.join('')}}\n")
59+
result.write("\\usepackage{graphicx}\n\\graphicspath{#{paths.map{ |p| "{#{p}}"}.join('')}}\n")
5860
result
5961
end
6062

@@ -67,19 +69,19 @@ def markdown_to_pdf(md:, pdf:)
6769
# Paths in README.md are relative to its location
6870
Dir.chdir(File.dirname(md)) do
6971
md = File.basename(md)
70-
metadatafile = extract_metadata_file(md)
72+
custom_defaults_file = extract_pandoc_defaults_file(md)
7173
gfx_paths_latex = generate_gfx_paths_latex([PATH_PANDOC_ROOT])
7274
run(
7375
'pandoc',
7476
"--include-in-header=#{gfx_paths_latex}",
77+
"--variable=date:#{get_change_date(md)}",
7578
"--defaults=#{PATH_PANDOC_ROOT / 'defaults_common.yaml'}",
7679
"--defaults=#{PATH_PANDOC_ROOT / 'defaults_pdf.yaml'}",
77-
"--variable=date:#{get_change_date(md)}",
78-
"--metadata-file=#{metadatafile}",
80+
"--defaults=#{custom_defaults_file}",
7981
"--output=#{pdf}",
8082
md
8183
)
82-
metadatafile.delete
84+
custom_defaults_file.delete
8385
gfx_paths_latex.delete
8486
end
8587
end
@@ -99,3 +101,78 @@ def markdown_to_html(md:, html:)
99101
)
100102
end
101103
end
104+
105+
# Main execution block when script is run directly
106+
if __FILE__ == $0
107+
# Print usage information
108+
def print_usage
109+
warn(<<~USAGE)
110+
Usage: #{File.basename($0)} <command> [arguments]
111+
112+
Commands:
113+
deps List pandoc dependency files
114+
pdf <input.md> <output.pdf> Convert Markdown to PDF
115+
html <input.md> <output.html> Convert Markdown to HTML
116+
117+
Examples:
118+
#{File.basename($0)} deps
119+
#{File.basename($0)} pdf README.md output.pdf
120+
#{File.basename($0)} html README.md output.html
121+
USAGE
122+
end
123+
124+
# Validate arguments and execute conversion
125+
begin
126+
# Check minimum number of arguments
127+
if ARGV.empty?
128+
print_usage
129+
exit(1)
130+
end
131+
132+
command = ARGV[0].downcase
133+
134+
# Handle 'deps' command
135+
if command == 'deps'
136+
puts PANDOC_DEPS.join(' ')
137+
exit(0)
138+
end
139+
140+
# Handle conversion commands (pdf, html)
141+
if ARGV.length != 3
142+
warn('Error: Conversion commands require exactly 3 arguments.')
143+
print_usage
144+
exit(1)
145+
end
146+
147+
input_file = ARGV[1]
148+
output_file = ARGV[2]
149+
150+
# Validate format
151+
unless %w[pdf html].include?(command)
152+
warn("Error: Invalid command '#{ARGV[0]}'. Must be 'deps', 'pdf', or 'html'.")
153+
print_usage
154+
exit(1)
155+
end
156+
157+
# Check if input file exists
158+
unless File.exist?(input_file)
159+
warn("Error: Input file '#{input_file}' not found.")
160+
exit(2)
161+
end
162+
163+
# Execute conversion based on format
164+
case command
165+
when 'pdf'
166+
markdown_to_pdf(md: input_file, pdf: output_file)
167+
when 'html'
168+
markdown_to_html(md: input_file, html: output_file)
169+
end
170+
171+
puts "Successfully converted #{input_file} to #{output_file}"
172+
exit(0)
173+
rescue => e
174+
warn("Error during conversion: #{e.message}")
175+
warn(e.backtrace.join("\n")) if ENV['DEBUG']
176+
exit(3)
177+
end
178+
end

0 commit comments

Comments
 (0)