Debian Codegraph @

texk/texlive/linked_scripts/markdown/markdown2tex.lua

raw symbols SCIP
1 #!/usr/bin/env texlua
2 --
3 -- Copyright (C) 2009-2016 John MacFarlane, Hans Hagen
4 --
5 -- Permission is hereby granted, free of charge, to any person obtaining
6 -- a copy of this software and associated documentation files (the
7 -- "Software"), to deal in the Software without restriction, including
8 -- without limitation the rights to use, copy, modify, merge, publish,
9 -- distribute, sublicense, and/or sell copies of the Software, and to
10 -- permit persons to whom the Software is furnished to do so, subject to
11 -- the following conditions:
12 --
13 -- The above copyright notice and this permission notice shall be
14 -- included in all copies or substantial portions of the Software.
15 --
16 -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 -- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 -- MERCHANTABILITY, FITNESS FOR PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 -- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 -- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 -- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 -- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 --
24 -- Copyright (C) 2016-2026 Vít Starý Novotný, Andrej Genčur
25 --
26 -- This work may be distributed and/or modified under the
27 -- conditions of the LaTeX Project Public License, either version 1.3
28 -- of this license or (at your option) any later version.
29 -- The latest version of this license is in
30 --
31 -- http://www.latex-project.org/lppl.txt
32 --
33 -- and version 1.3 or later is part of all distributions of LaTeX
34 -- version 2005/12/01 or later.
35 --
36 -- This work has the LPPL maintenance status `maintained'.
37 -- The Current Maintainer of this work is Vít Starý Novotný.
38 --
39 -- Send bug reports, requests for additions and questions
40 -- either to the GitHub issue tracker at
41 --
42 -- https://github.com/witiko/markdown/issues
43 --
44 -- or to the e-mail address <[email protected]>.
45 --
46 -- MODIFICATION ADVICE:
47 --
48 -- If you want to customize this file, it is best to make a copy of
49 -- the source file(s) from which it was produced. Use a different
50 -- name for your copy(ies) and modify the copy(ies); this will ensure
51 -- that your modifications do not get overwritten when you install a
52 -- new release of the standard system. You should also ensure that
53 -- your modified source file does not generate any modified file with
54 -- the same name as a standard file.
55 --
56 -- You will also need to produce your own, suitably named, .ins file to
57 -- control the generation of files from your source file; this file
58 -- should contain your own preambles for the files it generates, not
59 -- those in the standard .ins files.
60 --
61 local metadata = {
62 version = "3.13.0-0-gdd212d58",
63 comment = "A module for the conversion from markdown "
64 .. "to plain TeX",
65 author = "John MacFarlane, Hans Hagen, Vít Starý Novotný, "
66 .. "Andrej Genčur",
67 copyright = {"2009-2016 John MacFarlane, Hans Hagen",
68 "2016-2026 Vít Starý Novotný, Andrej Genčur"},
69 license = "LPPL 1.3c"
70 }
71
72 local defaultOptions = {}
73 local experimentalOptions = {}
74 setmetatable(experimentalOptions, { __index = function (_, key)
75 return defaultOptions[key] end })
76 defaultOptions.eagerCache = true
77 defaultOptions.experimental = false
78 defaultOptions.singletonCache = true
79 defaultOptions.unicodeNormalization = true
80 defaultOptions.unicodeNormalizationForm = "nfc"
81 defaultOptions.cacheDir = "."
82 defaultOptions.contentBlocksLanguageMap = "markdown-languages.json"
83 defaultOptions.debugExtensionsFileName = "debug-extensions.json"
84 defaultOptions.frozenCacheFileName = "frozenCache.tex"
85 defaultOptions.autoIdentifiers = false
86 defaultOptions.blankBeforeBlockquote = false
87 defaultOptions.blankBeforeCodeFence = false
88 defaultOptions.blankBeforeDivFence = false
89 defaultOptions.blankBeforeHeading = false
90 defaultOptions.blankBeforeHtmlBlock = false
91 defaultOptions.blankBeforeList = false
92 defaultOptions.bracketedSpans = false
93 defaultOptions.breakableBlockquotes = true
94 defaultOptions.citationNbsps = true
95 defaultOptions.citations = false
96 defaultOptions.codeSpans = true
97 defaultOptions.contentBlocks = false
98 defaultOptions.contentLevel = "block"
99 defaultOptions.debugExtensions = false
100 defaultOptions.definitionLists = false
101 defaultOptions.ensureJekyllData = false
102 defaultOptions.expectJekyllData = false
103 defaultOptions.extensions = {}
104 defaultOptions.fancyLists = false
105 defaultOptions.fencedCode = true
106 defaultOptions.fencedCodeAttributes = false
107 defaultOptions.fencedDivs = false
108 defaultOptions.finalizeCache = false
109 defaultOptions.frozenCacheCounter = 0
110 defaultOptions.gfmAutoIdentifiers = false
111 defaultOptions.hashEnumerators = false
112 defaultOptions.headerAttributes = false
113 defaultOptions.html = true
114 defaultOptions.htmlOverLinks = false
115 experimentalOptions.htmlOverLinks = true
116 defaultOptions.hybrid = false
117 defaultOptions.inlineCodeAttributes = false
118 defaultOptions.inlineNotes = false
119 defaultOptions.jekyllData = false
120 defaultOptions.linkAttributes = false
121 defaultOptions.lineBlocks = false
122 defaultOptions.mark = false
123 defaultOptions.notes = false
124 defaultOptions.pipeTables = false
125 defaultOptions.preserveTabs = true
126 defaultOptions.rawAttribute = false
127 defaultOptions.relativeReferences = false
128 defaultOptions.shiftHeadings = 0
129 defaultOptions.slice = "^ $"
130 defaultOptions.smartEllipses = false
131 defaultOptions.startNumber = true
132 defaultOptions.strikeThrough = false
133 defaultOptions.stripIndent = false
134 defaultOptions.subscripts = false
135 defaultOptions.superscripts = false
136 defaultOptions.tableAttributes = false
137 defaultOptions.tableCaptions = false
138 defaultOptions.taskLists = false
139 defaultOptions.texComments = false
140 defaultOptions.texMathDollars = false
141 defaultOptions.texMathDoubleBackslash = false
142 defaultOptions.texMathSingleBackslash = false
143 defaultOptions.tightLists = true
144 defaultOptions.underscores = true
145 local HELP_STRING = "Usage: " .. [[
146 markdown2tex [OPTIONS] -- [INPUT_FILE] [OUTPUT_FILE]
147
148 OPTIONS are documented in Section 2.2.1 of the Markdown Package User
149 Manual (https://ctan.org/pkg/markdown).
150
151 When OUTPUT_FILE is unspecified, the result of the conversion will be
152 written to the standard output. When INPUT_FILE is also unspecified, the
153 result of the conversion will be read from the standard input.
154
155 Report bugs to: [email protected]
156 Markdown package home page: <https://github.com/witiko/markdown>]]
157
158 local VERSION_STRING = [[
159 markdown2tex (Markdown) ]] .. metadata.version .. [[
160
161 Copyright (C) ]] .. table.concat(metadata.copyright,
162 "\nCopyright (C) ") .. [[
163
164 License: ]] .. metadata.license
165
166 local function warn(s)
167 io.stderr:write("Warning: " .. s .. "\n")
168 end
169
170 local function error(s)
171 io.stderr:write("Error: " .. s .. "\n")
172 os.exit(1)
173 end
174 local function camel_case(option_name)
175 local cased_option_name = option_name:gsub("_(%l)", function(match)
176 return match:sub(2, 2):upper()
177 end)
178 return cased_option_name
179 end
180
181 local function snake_case(option_name)
182 local cased_option_name = option_name:gsub("%l%u", function(match)
183 return match:sub(1, 1) .. "_" .. match:sub(2, 2):lower()
184 end)
185 return cased_option_name
186 end
187
188 local cases = {camel_case, snake_case}
189 local various_case_options = {}
190 for option_name, _ in pairs(defaultOptions) do
191 for _, case in ipairs(cases) do
192 various_case_options[case(option_name)] = option_name
193 end
194 end
195
196 local process_options = true
197 local options = {}
198 local input_filename
199 local output_filename
200 for i = 1, #arg do
201 if process_options then
202 if arg[i] == "--" then
203 process_options = false
204 goto continue
205 elseif arg[i]:match("=") then
206 local key, value = arg[i]:match("(.-)=(.*)")
207 if defaultOptions[key] == nil and
208 various_case_options[key] ~= nil then
209 key = various_case_options[key]
210 end
211 local default_type = type(defaultOptions[key])
212 if default_type == "boolean" then
213 options[key] = (value == "true")
214 elseif default_type == "number" then
215 options[key] = tonumber(value)
216 elseif default_type == "table" then
217 options[key] = {}
218 for item in value:gmatch("[^ ,]+") do
219 table.insert(options[key], item)
220 end
221 else
222 if default_type ~= "string" then
223 if default_type == "nil" then
224 warn('Option "' .. key .. '" not recognized.')
225 else
226 warn('Option "' .. key .. '" type not recognized, ' ..
227 'please file a report to the package maintainer.')
228 end
229 warn('Parsing the ' .. 'value "' .. value ..'" of option "' ..
230 key .. '" as a string.')
231 end
232 options[key] = value
233 end
234 goto continue
235 elseif arg[i] == "--help" or arg[i] == "-h" then
236 print(HELP_STRING)
237 os.exit()
238 elseif arg[i] == "--version" or arg[i] == "-v" then
239 print(VERSION_STRING)
240 os.exit()
241 end
242 end
243 if input_filename == nil then
244 input_filename = arg[i]
245 elseif output_filename == nil then
246 output_filename = arg[i]
247 else
248 error('Unexpected argument: "' .. arg[i] .. '".')
249 end
250 ::continue::
251 end
252
253 local input
254 if input_filename then
255 local input_file = assert(io.open(input_filename, "r"),
256 [[Could not open file "]] .. input_filename .. [[" for reading]])
257 input = assert(input_file:read("*a"))
258 assert(input_file:close())
259 else
260 input = assert(io.read("*a"))
261 end
262
263 local lfs = require("lfs")
264 if options.cacheDir and not lfs.isdir(options.cacheDir) then
265 assert(lfs.mkdir(options["cacheDir"]))
266 end
267 local kpse
268 (function()
269 local should_initialize = package.loaded.kpse == nil
270 or tex.initialize ~= nil
271 kpse = require("kpse")
272 if should_initialize then
273 kpse.set_program_name("luatex")
274 end
275 end)()
276 local md = require("markdown")
277 if metadata.version ~= md.metadata.version then
278 warn("markdown-cli.lua " .. metadata.version .. " used with " ..
279 "markdown.lua " .. md.metadata.version .. ".")
280 end
281
282 local convert = md.new(options)
283 local raw_output, flat_output = convert(input, true)
284 local output
285 if flat_output == nil then
286 if options.eagerCache then
287 warn("markdown.lua has not produced flat output, so I am using " ..
288 "backwards-compatible raw output instead. This may cause " ..
289 'the conversion result to be hidden behind "\\input".')
290 end
291 output = raw_output
292 else
293 output = flat_output()
294 end
295
296 if output_filename then
297 local output_file = assert(io.open(output_filename, "w"),
298 [[Could not open file "]] .. output_filename .. [[" for writing]])
299 assert(output_file:write(output))
300 assert(output_file:close())
301 else
302 assert(io.write(output))
303 end
304 if options.cacheDir then
305 lfs.rmdir(options.cacheDir)
306 end