@@ -2,8 +2,16 @@ local path = require 'pandoc.path'
22local utils = require ' pandoc.utils'
33local stringify = utils .stringify
44
5+ local function log (msg )
6+ io.stderr :write (' [Docs.lua filter: ERROR] ' .. msg .. ' \n ' )
7+ end
8+
59local function read_file (filename )
610 local fh = io.open (filename )
11+ if not fh then
12+ log (' Cannot read file ' .. filename .. ' .' )
13+ return
14+ end
715 local content = fh :read (' *a' )
816 fh :close ()
917 return content
@@ -17,8 +25,11 @@ local formats_by_extension = {
1725 html = ' html' ,
1826}
1927
20- local function sample_blocks (sample_file )
28+ local function sample_blocks (sample_file )
2129 local sample_content = read_file (sample_file )
30+ if not sample_content then
31+ return nil
32+ end
2233 local extension = select (2 , path .split_extension (sample_file )):sub (2 )
2334 local format = formats_by_extension [extension ] or extension
2435 local filename = path .filename (sample_file )
@@ -30,17 +41,16 @@ local function sample_blocks (sample_file)
3041 }
3142end
3243
33- local function result_block_raw (result_file , format )
34- local result_content = read_file (result_file )
35-
44+ local function result_block_raw (result_content , format )
3645 return pandoc .CodeBlock (result_content ,
3746 pandoc .Attr (' ' , {format , ' sample' })
3847 )
3948end
4049
4150local function result_block_html (filename )
4251 local html = ' <iframe width=100% height=720px '
43- .. ' src="' .. filename .. ' " sandbox>\n '
52+ -- ..'src="'..filename..'" sandbox>\n'
53+ .. ' src="' .. filename .. ' ">\n ' -- non-sandbox to display linked images
4454 .. ' <p><a href="' .. filename .. ' ">Click to see file</a></p>\n '
4555 .. ' </iframe>'
4656 return pandoc .RawBlock (' html' , html )
@@ -50,56 +60,69 @@ local function result_blocks(result_file)
5060 local extension = select (2 , path .split_extension (result_file )):sub (2 )
5161 local format = formats_by_extension [extension ] or extension
5262 local filename = path .filename (result_file )
53- local result = pandoc .List :new ({
54- pandoc .Header (3 ,
55- pandoc .Link (pandoc .Str (filename ), filename ),
56- {filename }
57- )
58- })
63+ local result = nil
5964
60- if format == ' html' then
61- result : insert ( result_block_html (filename ) )
65+ if format == ' html' then
66+ result = result_block_html (filename )
6267 else
63- result : insert ( result_block_raw (result_file , format ) )
68+ result = result_block_raw (result_file , format )
6469 end
6570
66- return result
67- end
71+ if result then
72+ return pandoc .List :new {
73+ pandoc .Header (3 ,
74+ pandoc .Link (pandoc .Str (filename ), filename ),
75+ { id = filename }
76+ ),
77+ result
78+ }
79+ end
6880
81+ end
6982
7083local function code_blocks (code_file )
7184 local code_content = read_file (code_file )
72- local code_attr = pandoc .Attr (code_file , {' lua' })
73- return {
74- pandoc .CodeBlock (code_content , code_attr )
75- }
85+ if code_content then
86+ local code_attr = pandoc .Attr (code_file , {' lua' })
87+ return {
88+ pandoc .CodeBlock (code_content , code_attr )
89+ }
90+ end
7691end
7792
7893function Pandoc (doc )
7994 local meta = doc .meta
8095 local blocks = doc .blocks
8196
82- -- Set document title from README title. There should usually be just
83- -- a single level 1 heading.
84- blocks = blocks :walk {
85- Header = function (h )
86- if h .level == 1 then
87- meta .title = h .content
88- return {}
97+ -- Ensure the document has a title: already set or first level 1 heading.
98+ if not meta .title then
99+ blocks = blocks :walk {
100+ Header = function (h )
101+ if h .level == 1 and not meta .title then
102+ meta .title = h .content
103+ return {}
104+ end
89105 end
90- end
91- }
106+ }
107+ end
92108
93- -- Add the sample file as an example.
94- blocks :extend {pandoc .Header (2 , ' Example' , pandoc .Attr (' Example' ))}
95- blocks :extend (sample_blocks (stringify (meta [' sample-file' ])))
96- blocks :extend (result_blocks (stringify (meta [' result-file' ])))
109+ -- Add the sample file source and result as an example if both present.
110+ local spl_blocks = sample_blocks (stringify (meta [' sample-file' ]))
111+ local res_blocks = result_blocks (stringify (meta [' result-file' ]))
112+ if spl_blocks and res_blocks then
113+ blocks :extend {pandoc .Header (2 , ' Example' , pandoc .Attr (' Example' ))}
114+ blocks :extend (spl_blocks )
115+ blocks :extend (res_blocks )
116+ end
97117
98118 -- Add the filter code.
99119 local code_file = stringify (meta [' code-file' ])
100- blocks :extend {pandoc .Header (2 , ' Code' , pandoc .Attr (' Code' ))}
101- blocks :extend {pandoc .Para {pandoc .Link (pandoc .Str (code_file ), code_file )}}
102- blocks :extend (code_blocks (code_file ))
120+ local cde_blocks = code_blocks (code_file )
121+ if cde_blocks then
122+ blocks :extend {pandoc .Header (2 , ' Code' , pandoc .Attr (' Code' ))}
123+ blocks :extend {pandoc .Para {pandoc .Link (pandoc .Str (code_file ), code_file )}}
124+ blocks :extend (cde_blocks )
125+ end
103126
104127 return pandoc .Pandoc (blocks , meta )
105128end
0 commit comments