Skip to content

Commit

Permalink
add --cmd-api and --cmd-rather-than-url
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanpoelen committed Jul 14, 2019
1 parent 9e4415c commit abb59f9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ ln -s /usr/include/x86_64-linux-gnu/ /tmp/include
luarocks --local install Lua-cURL CURL_DIR=/tmp/
```

As a last resort, you can remove the dependency on Lua-cURL and add `-c` to the `gh-md-toc` launch. If you do not have the `curl` program, you will also need to configure a command with `--cmd-api`.

Now you can restart the installation commands from the previous chapter.

## Example
Expand Down
2 changes: 1 addition & 1 deletion gh-md-toc-1.2-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ description = {
dependencies = {
"lua >= 5.1",
"lpeg >= 1.0",
"lua-curl >= 0.3",
"lua-curl >= 0.3", -- optional with the -c parameter (see gm-md-toc --help)
"argparse >= 0.6",
}
build = {
Expand Down
44 changes: 28 additions & 16 deletions gh-md-toc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ parser:option('--label-rename-title', 'Rename the title under this line that mat
parser:option('--label-start-toc', 'Writes the table of contents between label-start-toc and label-stop-toc (only with --inplace)', '<!-- toc -->'):argname'<line>'
parser:option('--label-stop-toc', 'Writes the table of contents between label-start-toc and label-stop-toc (only with --inplace)', '<!-- /toc -->'):argname'<line>'
parser:option('--url-api', 'Github API URL', 'https://api.github.com/markdown/raw'):argname'<url>'
parser:option('--cmd-api', 'Command for Github API', 'curl https://api.github.com/markdown/raw -X POST -H \'Content-Type: text/plain\' -s -d'):argname'<cmd>'
parser:flag2('-c --use-cmd-api', 'Use value of --cmd-api rather than --url-api')
parser:flag('--version', 'Output version information and exit'):action(function()
print('gh-md-toc 1.2.0')
os.exit(0)
Expand Down Expand Up @@ -218,23 +220,30 @@ end
min_depth_title = min_depth_title - 1

local url_api = args.url_api
local cmd_api = args.use_cmd_api and args.cmd_api ~= '' and args.cmd_api
local print_ln = '\n'

if url_api ~= '' then
local curl = require'cURL'

local html = {}
curl.easy{
url=url_api,
writefunction=function(s) html[#html+1] = s end,
httpheader={
'User-Agent: gh-md-toc',
'Content-Type: text/plain'
},
postfields=table.concat(titles, '\n'),
}
:perform()
:close()
if url_api ~= '' or cmd_api then
local md_titles = table.concat(titles, '\n')
local html = {} -- then string

if cmd_api then
html = io.popen(cmd_api .. " '" .. md_titles:gsub("'", "\\'") .. "'"):read('*a')
else
require'cURL'.easy{
url=url_api,
writefunction=function(s) html[#html+1] = s end,
httpheader={
'User-Agent: gh-md-toc',
'Content-Type: text/plain'
},
postfields=md_titles,
}
:perform()
:close()

html = table.concat(html)
end

function Formater(str)
local tos = function(x)
Expand Down Expand Up @@ -460,8 +469,11 @@ if url_api ~= '' then
toc[#toc+1] = format(datas)
datas.isfirst = false
end)^1
GhMdTitle:match(table.concat(html))
GhMdTitle:match(html)

if #titles ~= #toc then
error('invalid API result:\n' .. html)
end
titles = toc
print_ln = nil

Expand Down

0 comments on commit abb59f9

Please sign in to comment.