Debian Codegraph @

texmf-dist/tex/luatex/markdown/markdown.lua

raw symbols SCIP
1 --
2 -- Copyright (C) 2009-2016 John MacFarlane, Hans Hagen
3 --
4 -- Permission is hereby granted, free of charge, to any person obtaining
5 -- a copy of this software and associated documentation files (the
6 -- "Software"), to deal in the Software without restriction, including
7 -- without limitation the rights to use, copy, modify, merge, publish,
8 -- distribute, sublicense, and/or sell copies of the Software, and to
9 -- permit persons to whom the Software is furnished to do so, subject to
10 -- the following conditions:
11 --
12 -- The above copyright notice and this permission notice shall be
13 -- included in all copies or substantial portions of the Software.
14 --
15 -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 -- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 -- MERCHANTABILITY, FITNESS FOR PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 -- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 -- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 -- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 -- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 --
23 -- Copyright (C) 2016-2026 Vít Starý Novotný, Andrej Genčur
24 --
25 -- This work may be distributed and/or modified under the
26 -- conditions of the LaTeX Project Public License, either version 1.3
27 -- of this license or (at your option) any later version.
28 -- The latest version of this license is in
29 --
30 -- http://www.latex-project.org/lppl.txt
31 --
32 -- and version 1.3 or later is part of all distributions of LaTeX
33 -- version 2005/12/01 or later.
34 --
35 -- This work has the LPPL maintenance status `maintained'.
36 -- The Current Maintainer of this work is Vít Starý Novotný.
37 --
38 -- Send bug reports, requests for additions and questions
39 -- either to the GitHub issue tracker at
40 --
41 -- https://github.com/witiko/markdown/issues
42 --
43 -- or to the e-mail address <[email protected]>.
44 --
45 -- MODIFICATION ADVICE:
46 --
47 -- If you want to customize this file, it is best to make a copy of
48 -- the source file(s) from which it was produced. Use a different
49 -- name for your copy(ies) and modify the copy(ies); this will ensure
50 -- that your modifications do not get overwritten when you install a
51 -- new release of the standard system. You should also ensure that
52 -- your modified source file does not generate any modified file with
53 -- the same name as a standard file.
54 --
55 -- You will also need to produce your own, suitably named, .ins file to
56 -- control the generation of files from your source file; this file
57 -- should contain your own preambles for the files it generates, not
58 -- those in the standard .ins files.
59 --
60 local metadata = {
61 version = "3.15.0-0-g58d7bf4d",
62 comment = "A module for the conversion from markdown "
63 .. "to plain TeX",
64 author = "John MacFarlane, Hans Hagen, Vít Starý Novotný, "
65 .. "Andrej Genčur",
66 copyright = {"2009-2016 John MacFarlane, Hans Hagen",
67 "2016-2026 Vít Starý Novotný, Andrej Genčur"},
68 license = "LPPL 1.3c"
69 }
70
71 local md5 = require("md5")
72 local M = {metadata = metadata}
73 local defaultOptions = {}
74 local experimentalOptions = {}
75 setmetatable(experimentalOptions, { __index = function (_, key)
76 return defaultOptions[key] end })
77 defaultOptions.eagerCache = true
78 defaultOptions.experimental = false
79 defaultOptions.singletonCache = true
80 defaultOptions.unicodeNormalization = true
81 defaultOptions.unicodeNormalizationForm = "nfc"
82 defaultOptions.cacheDir = "."
83 defaultOptions.contentBlocksLanguageMap = "markdown-languages.json"
84 defaultOptions.debugExtensionsFileName = "debug-extensions.json"
85 defaultOptions.frozenCacheFileName = "frozenCache.tex"
86 defaultOptions.acronyms = {}
87 defaultOptions.autoIdentifiers = false
88 defaultOptions.blankBeforeBlockquote = false
89 defaultOptions.blankBeforeCodeFence = false
90 defaultOptions.blankBeforeDivFence = false
91 defaultOptions.blankBeforeHeading = false
92 defaultOptions.blankBeforeHtmlBlock = false
93 defaultOptions.blankBeforeList = false
94 defaultOptions.bracketedSpans = false
95 defaultOptions.breakableBlockquotes = true
96 defaultOptions.citationNbsps = true
97 defaultOptions.citations = false
98 defaultOptions.codeSpans = true
99 defaultOptions.contentBlocks = false
100 defaultOptions.contentLevel = "block"
101 defaultOptions.debugExtensions = false
102 defaultOptions.definitionLists = false
103 defaultOptions.ensureJekyllData = false
104 defaultOptions.expectJekyllData = false
105 defaultOptions.extensions = {}
106 defaultOptions.fancyLists = false
107 defaultOptions.fencedCode = true
108 defaultOptions.fencedCodeAttributes = false
109 defaultOptions.fencedDivs = false
110 defaultOptions.finalizeCache = false
111 defaultOptions.frozenCacheCounter = 0
112 defaultOptions.gfmAutoIdentifiers = false
113 defaultOptions.hashEnumerators = false
114 defaultOptions.headerAttributes = false
115 defaultOptions.html = true
116 defaultOptions.htmlOutput = "basic"
117 defaultOptions.htmlOverLinks = false
118 experimentalOptions.htmlOverLinks = true
119 defaultOptions.hybrid = false
120 defaultOptions.inlineCodeAttributes = false
121 defaultOptions.inlineNotes = false
122 defaultOptions.jekyllData = false
123 defaultOptions.linkAttributes = false
124 defaultOptions.lineBlocks = false
125 defaultOptions.mark = false
126 defaultOptions.notes = false
127 defaultOptions.pipeTables = false
128 defaultOptions.preserveTabs = true
129 defaultOptions.rawAttribute = false
130 defaultOptions.relativeReferences = false
131 defaultOptions.shiftHeadings = 0
132 defaultOptions.slice = "^ $"
133 defaultOptions.smartEllipses = false
134 defaultOptions.startNumber = true
135 defaultOptions.strikeThrough = false
136 defaultOptions.stripIndent = false
137 defaultOptions.subscripts = false
138 defaultOptions.superscripts = false
139 defaultOptions.tableAttributes = false
140 defaultOptions.tableCaptions = false
141 defaultOptions.taskLists = false
142 defaultOptions.texComments = false
143 defaultOptions.texMathDollars = false
144 defaultOptions.texMathDoubleBackslash = false
145 defaultOptions.texMathSingleBackslash = false
146 defaultOptions.tightLists = true
147 defaultOptions.underscores = true
148 local util = {}
149 function util.cache(dir, string, salt, transform, suffix)
150 local digest = md5.sumhexa(string .. (salt or ""))
151 local name = util.pathname(dir, digest .. suffix)
152 local file = io.open(name, "r")
153 local result = nil
154 if file == nil then -- If no cache entry exists, create a new one.
155 file = assert(io.open(name, "w"),
156 [[Could not open file "]] .. name .. [[" for writing]])
157 result = string
158 if transform ~= nil then
159 result = transform(result)
160 end
161 assert(file:write(result))
162 assert(file:close())
163 end
164 return name, result
165 end
166 function util.pathname(dir, file)
167 if #dir == 0 then
168 return file
169 else
170 return dir .. "/" .. file
171 end
172 end
173 function util.salt(options)
174 local opt_string = {}
175 for k, _ in pairs(defaultOptions) do
176 local v = options[k]
177 if type(v) == "table" then
178 for _, i in ipairs(v) do
179 opt_string[#opt_string+1] = k .. "=" .. tostring(i)
180 end
181 elseif k ~= "cacheDir" then
182 opt_string[#opt_string+1] = k .. "=" .. tostring(v)
183 end
184 end
185 table.sort(opt_string)
186 local salt = table.concat(opt_string, ",")
187 .. "," .. metadata.version
188 return salt
189 end
190 util.warning = (function()
191 local function warning(s)
192 io.stderr:write("Warning: " .. s .. "\n")
193 end
194 return warning
195 end)()
196 function M.new(options)
197 options = options or {}
198 setmetatable(options, { __index = function (_, key)
199 return defaultOptions[key] end })
200 if options.experimental then
201 setmetatable(options, { __index = function (_, key)
202 return experimentalOptions[key] end })
203 end
204 local parser_convert = nil
205 return function(input, include_flat_output)
206 local function convert(input)
207 if parser_convert == nil then
208 local parser = require("markdown-parser")
209 if metadata.version ~= parser.metadata.version then
210 warn("markdown.lua " .. metadata.version .. " used with " ..
211 "markdown-parser.lua " .. parser.metadata.version .. ".")
212 end
213 parser_convert = parser.new(options)
214 end
215 return parser_convert(input)
216 end
217 local raw_output, flat_output
218 if options.eagerCache or options.finalizeCache then
219 local salt = util.salt(options)
220 local name, result = util.cache(options.cacheDir, input, salt,
221 convert, ".md.tex")
222 raw_output = [[\input{]] .. name .. [[}\relax]]
223 flat_output = function()
224 if result == nil then
225 local input_file = assert(io.open(name, "r"),
226 [[Could not open file "]] .. name .. [[" for reading]])
227 result = assert(input_file:read("*a"))
228 assert(input_file:close())
229 end
230 return result
231 end
232 else
233 raw_output = convert(input)
234 flat_output = function()
235 return raw_output
236 end
237 end
238 if options.finalizeCache then
239 local file, mode
240 if options.frozenCacheCounter > 0 then
241 mode = "a"
242 else
243 mode = "w"
244 end
245 file = assert(io.open(options.frozenCacheFileName, mode),
246 [[Could not open file "]] .. options.frozenCacheFileName
247 .. [[" for writing]])
248 assert(file:write(
249 [[\expandafter\global\expandafter\def\csname ]]
250 .. [[markdownFrozenCache]] .. options.frozenCacheCounter
251 .. [[\endcsname{]] .. raw_output .. [[}]] .. "\n"))
252 assert(file:close())
253 end
254 if include_flat_output then
255 return raw_output, flat_output
256 else
257 return raw_output
258 end
259 end
260 end
261 return M