1local function unmarkdown(s) 2 s = s:gsub("#", "\\#") 3 s = s:gsub("- ", "\\- ") 4 s = s:gsub("<", "\\<") 5 s = s:gsub(">", "\\>") 6 s = s:gsub("`", "\\`") 7 s = s:gsub("_", "\\_") 8 s = s:gsub("*", "\\*") 9 return s 10end 11 12local style_tab = 13{ 14 ["H1"] = {false, '# ', '\n'}, 15 ["H2"] = {false, '## ', '\n'}, 16 ["H3"] = {false, '### ', '\n'}, 17 ["H4"] = {false, '#### ', '\n'}, 18 ["P"] = {false, '', '\n'}, 19 ["L"] = {false, '- ', ''}, 20 ["LB"] = {false, '- ', ''}, 21 ["Q"] = {false, '> ', '\n'}, 22 ["V"] = {false, '> ', '\n'}, 23 ["RAW"] = {false, ' ', ''}, 24 ["PRE"] = {true, '`', '`'} 25} 26 27local function callback(writer, document) 28 local currentpara = nil 29 30 function changepara(newpara) 31 local currentstyle = style_tab[currentpara] 32 local newstyle = style_tab[newpara] 33 34 if (newpara ~= currentpara) or 35 not newpara or 36 not currentstyle[1] or 37 not newstyle[1] 38 then 39 if currentstyle then 40 writer(currentstyle[3]) 41 end 42 writer("\n") 43 if newstyle then 44 writer(newstyle[2]) 45 end 46 currentpara = newpara 47 else 48 writer("\n") 49 end 50 end 51 52 return ExportFileUsingCallbacks(document, 53 { 54 prologue = function() 55 end, 56 57 rawtext = function(s) 58 writer(s) 59 end, 60 61 text = function(s) 62 writer(unmarkdown(s)) 63 end, 64 65 notext = function() 66 end, 67 68 italic_on = function() 69 writer("_") 70 end, 71 72 italic_off = function() 73 writer("_") 74 end, 75 76 underline_on = function() 77 end, 78 79 underline_off = function() 80 end, 81 82 bold_on = function() 83 writer("**") 84 end, 85 86 bold_off = function() 87 writer("**") 88 end, 89 90 list_start = function() 91 writer("\n") 92 end, 93 94 list_end = function() 95 writer("\n") 96 end, 97 98 paragraph_start = function(s) 99 changepara(s) 100 end, 101 102 paragraph_end = function(s) 103 end, 104 105 epilogue = function() 106 changepara(nil) 107 end, 108 }) 109end 110 111function Cmd.ExportMarkdownFile(filename) 112 return ExportFileWithUI(filename, "Export Markdown File", ".md", callback) 113end 114