1- -- Add the table and equation numbering that Pandoc's LaTeX reader does not
2- -- preserve completely in HTML. The report document class resets both
3- -- counters at every chapter, so the HTML numbers use chapter.counter too.
1+ -- Add the table, figure, and equation numbering that Pandoc's LaTeX reader
2+ -- does not preserve completely in HTML. The report document class resets
3+ -- all three counters at every chapter, so the HTML numbers use
4+ -- chapter.counter too.
45
56local chapter = 0
67local table_counter = 0
8+ local figure_counter = 0
79local equation_counter = 0
810local references = {}
911local source_table_numbers = {}
12+ local source_figure_numbers = {}
1013
1114local numbered_environments = {
1215 equation = " single" ,
@@ -104,7 +107,7 @@ local function next_source_structure(source, position)
104107 if chapter_start then
105108 candidates [# candidates + 1 ] = { chapter_start , chapter_end , " chapter" }
106109 end
107- for _ , environment in ipairs ({ " table" , " longtable" }) do
110+ for _ , environment in ipairs ({ " table" , " longtable" , " figure " }) do
108111 local start_at , end_at = source :find (" \\ begin%s*{" .. environment .. " }" , position )
109112 if start_at then
110113 candidates [# candidates + 1 ] = { start_at , end_at , environment }
@@ -114,7 +117,7 @@ local function next_source_structure(source, position)
114117 return candidates [1 ]
115118end
116119
117- local function collect_source_table_numbers ()
120+ local function collect_source_object_numbers ()
118121 local input_file = PANDOC_STATE .input_files [1 ]
119122 if not input_file or input_file == " -" then
120123 return
@@ -124,6 +127,7 @@ local function collect_source_table_numbers()
124127 local source = expanded_source (input_file , document_directory , {})
125128 local source_chapter = 0
126129 local source_table_counter = 0
130+ local source_figure_counter = 0
127131 local position = 1
128132
129133 while true do
@@ -135,6 +139,7 @@ local function collect_source_table_numbers()
135139 if item [3 ] == " chapter" then
136140 source_chapter = source_chapter + 1
137141 source_table_counter = 0
142+ source_figure_counter = 0
138143 position = item [2 ] + 1
139144 else
140145 local environment = item [3 ]
@@ -144,23 +149,34 @@ local function collect_source_table_numbers()
144149 else
145150 local body = source :sub (item [2 ] + 1 , environment_end )
146151 local has_numbered_caption = body :find (" \\ caption%s*[%[{]" ) ~= nil
147- if environment == " longtable" or has_numbered_caption then
148- source_table_counter = source_table_counter + 1
149- end
150- for label in body :gmatch (" \\ label%s*{(table:[^}]+)}" ) do
151- source_table_numbers [label ] = tostring (source_chapter ) .. " ." .. tostring (source_table_counter )
152+ if environment == " figure" then
153+ if has_numbered_caption then
154+ source_figure_counter = source_figure_counter + 1
155+ end
156+ for label in body :gmatch (" \\ label%s*{([^}]+)}" ) do
157+ source_figure_numbers [label ] = tostring (source_chapter ) .. " ." .. tostring (source_figure_counter )
158+ end
159+ else
160+ if environment == " longtable" or has_numbered_caption then
161+ source_table_counter = source_table_counter + 1
162+ end
163+ for label in body :gmatch (" \\ label%s*{([^}]+)}" ) do
164+ if label :match (" ^table:" ) or label :match (" ^tab:" ) then
165+ source_table_numbers [label ] = tostring (source_chapter ) .. " ." .. tostring (source_table_counter )
166+ end
167+ end
152168 end
153169 position = environment_end + 1
154170 end
155171 end
156172 end
157173end
158174
159- collect_source_table_numbers ()
175+ collect_source_object_numbers ()
160176
161- local function prepend_caption_number (caption , number )
177+ local function prepend_caption_number (caption , kind , number )
162178 local prefix = pandoc .Inlines ({
163- pandoc .Str (" Table " ),
179+ pandoc .Str (kind ),
164180 pandoc .Space (),
165181 pandoc .Str (number .. " :" ),
166182 pandoc .Space (),
@@ -328,6 +344,7 @@ local numbering_filter = {
328344 if el .level == 1 and not has_class (el , " unnumbered" ) then
329345 chapter = chapter + 1
330346 table_counter = 0
347+ figure_counter = 0
331348 equation_counter = 0
332349 end
333350 end ,
@@ -348,14 +365,44 @@ local numbering_filter = {
348365 number = number_string (table_counter )
349366 end
350367 el .attributes [" data-number" ] = number
351- prepend_caption_number (el .caption , number )
368+ prepend_caption_number (el .caption , " Table " , number )
352369
353370 if el .identifier ~= " " then
354371 references [el .identifier ] = { number = number , kind = " table" }
355372 end
356373 return el
357374 end ,
358375
376+ Figure = function (el )
377+ if pandoc .utils .stringify (el .caption ) == " " then
378+ return nil
379+ end
380+
381+ local number = source_figure_numbers [el .identifier ]
382+ if number then
383+ figure_counter = tonumber (number :match (" %.(%d+)$" )) or figure_counter
384+ else
385+ figure_counter = figure_counter + 1
386+ number = number_string (figure_counter )
387+ end
388+ el .attributes [" data-number" ] = number
389+ prepend_caption_number (el .caption , " Figure" , number )
390+
391+ if el .identifier ~= " " then
392+ references [el .identifier ] = { number = number , kind = " figure" }
393+ local remove_duplicate_anchor = {
394+ Span = function (span )
395+ if span .identifier == el .identifier then
396+ return {}
397+ end
398+ end ,
399+ }
400+ el .caption .long = el .caption .long :walk (remove_duplicate_anchor )
401+ el .content = el .content :walk (remove_duplicate_anchor )
402+ end
403+ return el
404+ end ,
405+
359406 Para = function (el )
360407 local meaningful = meaningful_inlines (el .content )
361408 if # meaningful == 1 and meaningful [1 ].t == " Math" and meaningful [1 ].mathtype == " DisplayMath" then
0 commit comments