Debian Codegraph @

inst/rmarkdown/lua/number-sections.lua

raw symbols SCIP
1 --[[
2 number-sections - Number sections like the --number-sections option
3
4 When output is a markdown flavor +gfm_auto_identifiers, pandoc must run twice
5 with this filter to keep alive links to numbered headers. In the first time,
6 add a following argument: `--metadata preprocess_number_sections=true`.
7
8 # MIT License
9
10 Copyright (c) 2020 Atsushi Yasumoto, Christophe Dervieux
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
13
14 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17
18 https://github.com/atusy/lua-filters/blob/master/lua/number-sections.lua
19 ]]
20
21 -- REQUIREMENTS: Load shared lua filter - see `shared.lua` for more details.
22 dofile(os.getenv 'RMARKDOWN_LUA_SHARED')
23
24 --[[
25 About the requirement:
26 * PANDOC_VERSION -> 2.1
27 ]]
28 if (not pandocAvailable {2,1}) then
29 io.stderr:write("[WARNING] (number-sections.lua) requires at least Pandoc 2.1. Lua Filter skipped.\n")
30 return {}
31 end
32
33 -- START OF THE FILTER'S FUNCTIONS --
34
35 local section_number_table = {0, 0, 0, 0, 0, 0, 0, 0, 0}
36 local n_section_number_table = #section_number_table
37 local previous_header_level = 0
38 local separator = pandoc.Space()
39 local identifiers = {}
40 if FORMAT == "docx" then -- to be consistent with Pandoc >= 2.10.1
41 separator = pandoc.Str("\t")
42 end
43
44
45 function is_preprocess(meta)
46 -- Check if the filter is run in preprocessing step
47 -- This should only be done for markdown document conversion
48 preprocess = meta['preprocess_number_sections']
49 return nil
50 end
51
52
53 function fix_link_id(link)
54 -- Some markdown formats needs fixing on there link to numbered sections
55 -- idenfiers is filled during processing when a pre processing has been run
56 if not preprocess and #identifiers and identifiers[link.target] then
57 link.target = identifiers[link.target]
58 return link
59 end
60 -- do nothing in all other cases
61 return nil
62 end
63
64
65 function number_sections(header)
66
67 ----------------------------
68 -- specific handling when pre-processing has been done
69 ----------------------------
70
71 -- This will only run on second conversion when a preprocessing has be run before
72 -- to add recorded attributes set by us as Span attribute in first processing.
73 if not preprocess and #header.content and (header.content[1].t == 'Span') then
74 -- Check attributes
75 local span_content = header.content[1]
76 local old_id = span_content.attributes['data-rmarkdown-temporarily-recorded-id']
77 -- If found, this is our Span added in pre processing step
78 if old_id then
79 identifiers['#' .. old_id] = '#' .. header.identifier
80 -- remove span
81 table.remove(header.content, 1)
82 -- prepend span Inlines content to the header content
83 header.content = span_content.content:__concat(header.content)
84 -- return content without the span
85 return header
86 end
87 -- Otherwise, not our spans so do nothing
88 return nil
89 end
90
91 -- The following part runs in pre-processing step or main processing step
92 -- depending on the format.
93 -- (md output require a pre processing and numbered section ared added on input).
94
95 -- If explicitly unnumbered, then we do nothing
96 if header.classes:find("unnumbered") then
97 return nil
98 end
99
100 ----------------------------
101 -- Adding the number section
102 ----------------------------
103
104 --- Reset and update section_number_table
105 if (header.level < previous_header_level) then
106 for i=header.level+1,n_section_number_table do
107 section_number_table[i] = 0
108 end
109 end
110 previous_header_level = header.level
111 section_number_table[header.level] = section_number_table[header.level] + 1
112
113 --- Define section number as string
114 local section_number_string = tostring(section_number_table[header.level])
115 if header.level > 1 then
116 for i = header.level-1,1,-1 do
117 section_number_string = section_number_table[i] .. "." .. section_number_string
118 end
119 end
120
121 --- Update Header element
122 table.insert(header.content, 1, separator)
123 table.insert(header.content, 1, pandoc.Str(section_number_string))
124
125 ----------------------------
126 -- Pre-processing specific handling
127 ----------------------------
128
129 --- Return Header wrapped in Spans with recording Header's current ID as attribute
130 if preprocess then
131 local attr = {}
132 attr['data-rmarkdown-temporarily-recorded-id'] = header.identifier
133 header.content = {pandoc.Span(
134 header.content,
135 pandoc.Attr('', {}, attr)
136 )}
137 end
138
139 return header
140 end
141
142
143 return {
144 {Meta = is_preprocess}, -- Are we running as pre-pocessing ?
145 {Header = number_sections}, -- Add Number to section
146 {Link = fix_link_id}, -- Only done when preprocessing has been run
147 }