-
Notifications
You must be signed in to change notification settings - Fork 0
/
marktex.lua
50 lines (42 loc) · 1.31 KB
/
marktex.lua
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
local parse = require "parse"
local write = require "write"
local default_config = require "default_config"
local fu = require "fileutils"
local self = {}
-- Main function
self.convert = function (input_path, cfg)
local config = default_config
if cfg then
for k, v in pairs(cfg) do
config[k] = v
end
end
-- Create mdtex directory if it does not exist
fu.create_directory(config.save_dir)
-- Output file path
local output_path = fu.get_output_filename(
input_path, config.save_dir)
-- Check if file has been modified since last conversion
local last_conversion = fu.getFirstLine(output_path)
local last_modified = fu.getLastModifiedTime(input_path)
if last_conversion == "% " .. last_modified then
return output_path
end
local content, err = fu.read_file(input_path)
if not content then
print(err)
return
end
local ast = parse(content, config)
local tex = write(ast, config)
-- Add last modified time to the first line of the output file
tex = "% " .. last_modified .. "\n" .. tex
local success, err = fu.save_file(output_path, tex)
if not success then
print(err)
else
print("\nFile processed and saved to " .. output_path)
end
return output_path
end
return self