1+ #!/usr/bin/env ruby
12# frozen_string_literal: true
23
34require 'pathname'
45require_relative 'paths'
56require_relative 'build_tools'
67
78include BuildTools
9+ include Paths
810
911# Path to pandoc root directory
1012PATH_PANDOC_ROOT = ENV . key? ( 'DIR_PANDOC' ) ? Pathname . new ( ENV [ 'DIR_PANDOC' ] ) : Paths ::PANDOC
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
3941end
4042
4143# Get latest git change date, or else just the file's modification date
@@ -54,7 +56,7 @@ def get_change_date(md)
5456def 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
5961end
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
8587end
@@ -99,3 +101,78 @@ def markdown_to_html(md:, html:)
99101 )
100102 end
101103end
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