Snusmumriken

Source MD grabber

Feb 23rd, 2020
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.41 KB | None | 0 0
  1. --[[
  2. > lua this_script.lua -i file1.c -i file2.h -o doc.md
  3. You can easily change regex and grab md text from any language
  4. (c) Snooze 2020.
  5. ]]
  6.  
  7. local function argparse(t)
  8.     local conf = {}
  9.     local i = 0
  10.     while i < #arg do
  11.         i = i + 1
  12.         local v = arg[i]
  13.         if v:find('^[\\-]') then
  14.             i = i + 1
  15.             local key = v:match('^[\\-]*(.*)')
  16.             local t = conf[key] or {}
  17.             table.insert(t, arg[i])
  18.             conf[key] = t
  19.         else
  20.             table.insert(conf, v)
  21.         end
  22.     end
  23.     return conf
  24. end
  25.  
  26. local function check(v, msg)
  27.     return v or (print(msg) and os.exit())
  28. end
  29.  
  30. local function readfile(filename)
  31.  
  32.     local file, err = io.open(filename, 'rb')
  33.     if not file then
  34.         print( ("Can't read %s file: %s"):format(filename, err) )
  35.         return nil, err
  36.     end
  37.     return file:read("*a"), file:close()
  38. end
  39.  
  40. local conf = argparse(arg)
  41.  
  42. local files = check(conf.i and #conf.i > 0 and conf.i, "No input files specified")
  43. local out   = (conf.o or {})[1] or "README.md"
  44. print("Files to process: \n  " .. table.concat(files, ',\n  '))
  45. print("Output file: \n  "      .. out)
  46.  
  47. local md_data = {}
  48.  
  49. print("Parsing")
  50. for i, filename in ipairs(files) do
  51.     print("  " .. filename)
  52.     local data = readfile(filename)
  53.     if data then
  54.         for line in data:gmatch("/%*%s?![mM][dD](.-)%*/") do
  55.             table.insert(md_data, line)
  56.         end
  57.     end
  58. end
  59.  
  60. local text = table.concat(md_data, '\n')
  61. out = check(io.open(out, 'wb'))
  62. out:write(text)
  63. out:close()
  64. print("OK")
Advertisement
Add Comment
Please, Sign In to add comment