mirror of
https://github.com/TangentFoxy/markdown.lua.git
synced 2025-07-27 17:42:16 +00:00
Refactor file reading
Fixes crash when input is a directory.
This commit is contained in:
39
markdown.lua
39
markdown.lua
@@ -1073,6 +1073,13 @@ function OptionParser:run(args)
|
|||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function read_file(path, descr)
|
||||||
|
local file = io.open(path) or error("Could not open " .. descr .. " file: " .. path)
|
||||||
|
local contents = file:read("*a") or error("Could not read " .. descr .. " from " .. path)
|
||||||
|
file:close()
|
||||||
|
return contents
|
||||||
|
end
|
||||||
|
|
||||||
-- Handles the case when markdown is run from the command line
|
-- Handles the case when markdown is run from the command line
|
||||||
local function run_command_line(arg)
|
local function run_command_line(arg)
|
||||||
-- Generate output for input s given options
|
-- Generate output for input s given options
|
||||||
@@ -1081,9 +1088,7 @@ local function run_command_line(arg)
|
|||||||
if not options.wrap_header then return s end
|
if not options.wrap_header then return s end
|
||||||
local header
|
local header
|
||||||
if options.header then
|
if options.header then
|
||||||
local f = io.open(options.header) or error("Could not open file: " .. options.header)
|
header = read_file(options.header, "header")
|
||||||
header = f:read("*a")
|
|
||||||
f:close()
|
|
||||||
else
|
else
|
||||||
header = [[
|
header = [[
|
||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||||
@@ -1099,13 +1104,7 @@ local function run_command_line(arg)
|
|||||||
s:match("<h3>(.-)</h3>") or "Untitled"
|
s:match("<h3>(.-)</h3>") or "Untitled"
|
||||||
header = header:gsub("TITLE", title)
|
header = header:gsub("TITLE", title)
|
||||||
if options.inline_style then
|
if options.inline_style then
|
||||||
local style = ""
|
local style = read_file(options.stylesheet, "style sheet")
|
||||||
local f = io.open(options.stylesheet)
|
|
||||||
if f then
|
|
||||||
style = f:read("*a") f:close()
|
|
||||||
else
|
|
||||||
error("Could not include style sheet " .. options.stylesheet .. ": File not found")
|
|
||||||
end
|
|
||||||
header = header:gsub('<link rel="stylesheet" type="text/css" href="STYLESHEET" />',
|
header = header:gsub('<link rel="stylesheet" type="text/css" href="STYLESHEET" />',
|
||||||
"<style type=\"text/css\"><!--\n" .. style .. "\n--></style>")
|
"<style type=\"text/css\"><!--\n" .. style .. "\n--></style>")
|
||||||
else
|
else
|
||||||
@@ -1115,9 +1114,7 @@ local function run_command_line(arg)
|
|||||||
end
|
end
|
||||||
local footer = "</body></html>"
|
local footer = "</body></html>"
|
||||||
if options.footer then
|
if options.footer then
|
||||||
local f = io.open(options.footer) or error("Could not open file: " .. options.footer)
|
footer = read_file(options.footer, "footer")
|
||||||
footer = f:read("*a")
|
|
||||||
f:close()
|
|
||||||
end
|
end
|
||||||
return header .. s .. footer
|
return header .. s .. footer
|
||||||
end
|
end
|
||||||
@@ -1186,15 +1183,13 @@ Other options:
|
|||||||
end)
|
end)
|
||||||
op:flag("h", "help", function() print(help) run_stdin = false end)
|
op:flag("h", "help", function() print(help) run_stdin = false end)
|
||||||
op:arg(function(path)
|
op:arg(function(path)
|
||||||
local file = io.open(path) or error("Could not open file: " .. path)
|
local s = read_file(path, "input")
|
||||||
local s = file:read("*a")
|
s = run(s, options)
|
||||||
file:close()
|
local file = io.open(outpath(path, options), "w") or error("Could not open output file: " .. outpath(path, options))
|
||||||
s = run(s, options)
|
file:write(s)
|
||||||
file = io.open(outpath(path, options), "w") or error("Could not open output file: " .. outpath(path, options))
|
file:close()
|
||||||
file:write(s)
|
run_stdin = false
|
||||||
file:close()
|
end
|
||||||
run_stdin = false
|
|
||||||
end
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if not op:run(arg) then
|
if not op:run(arg) then
|
||||||
|
Reference in New Issue
Block a user