trphoenix
2025-11-27 671e8fe727be08651992bb5f49b2680731543fc6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package.path=debug.getinfo(1).source:gsub('@',''):sub(0):match('(.*[/\\])'):sub(0) .. '?.lua' .. ';' .. package.path
 
require("polyfill")
local url = require('url')
 
local pandoc=pandoc
local PANDOC_STATE=PANDOC_STATE
 
PANDOC_VERSION:must_be_at_least '3.1.7'
 
os.text = pandoc.text
 
local PATH = pandoc.path
local doc_dir = nil
local media_dir = nil
 
if Mode == nil then
  Mode = 'default'
end
 
-- print("Mode: "..Mode)
 
if PANDOC_STATE.output_file then
  local output_file = PANDOC_STATE.output_file
  doc_dir = PATH.directory(output_file)
  if PANDOC_WRITER_OPTIONS.variables["media_dir"] then
    media_dir = tostring(PANDOC_WRITER_OPTIONS.variables["media_dir"])
  else
    media_dir = PATH.split_extension(output_file)
    if Mode ~= 'hugo' then
      media_dir = media_dir .. '-media'
    end
  end
end
assert(doc_dir, "doc_dir is nil")
assert(media_dir, "media_dir is nil")
 
 
local function get_absolute_path(file_path)
  if PATH.is_absolute(file_path) then
    return file_path
  end
  for _, dir in pairs(PANDOC_STATE.resource_path) do
    local full_path = PATH.join({dir, file_path})
    if os.exists(full_path) then
      return full_path
    end
  end
  for _, file in pairs(PANDOC_STATE.input_files) do
    if not PATH.is_absolute(file) then
      file = PATH.join({pandoc.system.get_working_directory(), file_path})
    end
    local dir = PATH.directory(file)
    local full_path = PATH.join({dir, file_path})
    if os.exists(full_path) then
      return full_path
    end
  end
  return nil
end
 
local function get_output_file(file_path)
  if media_dir then
    local new_file_name = pandoc.utils.sha1(file_path)
    local _, new_file_ext = PATH.split_extension(file_path)
    file_path = new_file_name .. new_file_ext
    local full_path = PATH.join({media_dir, file_path})
    return full_path
  else
    return nil
  end
end
 
local function extract_media(file_path)
  os.mkdir(media_dir)
  file_path = url.decode(file_path)
  local abs_path = get_absolute_path(file_path)
  local file = get_output_file(file_path)
  if abs_path and file then
    if not os.exists(file) then
      os.copy(abs_path, file)
    end
    local rel_path = PATH.make_relative(file, doc_dir, false)
    local parts = PATH.split(rel_path)
    for i,v in ipairs(parts) do
      parts[i] = url.encode(v)
    end
    local encoded_rel_path = table.concat(parts, "/")
    if Mode == 'hugo' then
      encoded_rel_path = '../' .. encoded_rel_path
    end
    return encoded_rel_path
  end
end
 
local function raw(s)
  return pandoc.RawInline('markdown', s)
end
 
function Image(el)
  local src = extract_media(el.src)
  if src then
    el.src = src
  end
  return el
end
 
function Space()
  return raw(' ')
end
 
function SoftBreak()
  return raw('\n')
end
 
function RawInline(el)
  if el.format == "html" then
    el.format = 'markdown'
    el.text = string.gsub(el.text, '<img[^>]+>', function(img)
      return string.gsub(img, 'src="([^"]+)"', function(url)
        if string.find(url, '^[Hh][Tt][Tt][Pp][Ss]?://') == nil  then
          local extract_media_url = extract_media(url)
          if extract_media_url then
            return 'src="' .. extract_media_url .. '"'
          end
          return '123'
        end
        return 'src="' .. url .. '"'
      end)
    end)
  end
  return el
end
 
function RawBlock(el)
  if el.format == "html" then
    el.format = 'markdown'
  end
  return el
end
 
function Math(el)
  if Mode == 'hugo' then
    if el.mathtype == 'DisplayMath' then
      return raw('{{< mathjax >}}\n$$'  .. el.text .. '$$\n{{</mathjax >}}')
    else
      el.text = string.gsub(el.text, '\\[\\{\\}]', function (v)
        return '\\' .. v
      end)
      el.text = string.gsub(el.text, '_', function (v)
        return '\\' .. v
      end)
    end
  end
  return el
end
 
local function headerLink(input)
  -- github style section link
  return "#"..input:gsub(' ', '-')
end
 
 
local function insertLink(content, linkDescription)
  local descriptionText = table.concat(linkDescription, "")
 
  if string.find(descriptionText, '|') then
    local target, desc = descriptionText:match("(.*)|(.*)")
    table.insert(content, pandoc.Link(desc, headerLink(target)))
  else
    table.insert(content, pandoc.Link(descriptionText, headerLink(descriptionText)))
  end
end
 
function Para(el)
  local content = el.content
  content = ProcessMath(content)
  content = ProcessInternalLinks(content)
  el.content = content
  return el
end
 
function ProcessMath(elements)
  local content = {}
  local in_display_math = false
  for _, item in pairs(elements) do
    if item.t == 'Str'and item.text == "$$" then
      in_display_math = not in_display_math
    else
      if in_display_math then
        if item.t == 'RawInline' and item.format == 'tex' then
          local n = pandoc.Math('DisplayMath', '\n' .. item.text .. '\n')
          table.insert(content, Math(n))
        else
          table.insert(content, item)
        end
      else
        table.insert(content, item)
      end
    end
  end
  return content
end
 
function ProcessInternalLinks(elements)
  local content = {}
  local in_section_link = false
  local linkDescription = {}
 
  for _, item in pairs(elements) do
    if item.t == 'Str' and string.starts_with(item.text, '[[#') then
      in_section_link = true
      table.insert(linkDescription, string.sub(item.text, 4))
    elseif in_section_link then
      if string.ends_with(item.text, ']]') then
        table.insert(linkDescription, string.sub(item.text, 1, -3))
        insertLink(content, linkDescription)
        in_section_link = false
        linkDescription = {}
      else
        table.insert(linkDescription, item.text)
      end
    else
      table.insert(content, item)
    end
  end
  return content
end
 
function Plain(el)
  el.content = ProcessInternalLinks(el.content)
  return el
end
 
function Pandoc(el)
  return el
end