diff --git a/markdown.lua b/markdown.lua index 6fc7dbf..636e8f9 100644 --- a/markdown.lua +++ b/markdown.lua @@ -1073,6 +1073,13 @@ function OptionParser:run(args) return true 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 local function run_command_line(arg) -- 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 local header if options.header then - local f = io.open(options.header) or error("Could not open file: " .. options.header) - header = f:read("*a") - f:close() + header = read_file(options.header, "header") else header = [[ @@ -1099,13 +1104,7 @@ local function run_command_line(arg) s:match("