Skip to content

Commit 1bfc5d6

Browse files
committed
Update examples to be consistent with plugin
Add Path, utils, Context, Config, and Section to require"crazywall" Remove preserve from :CrazywallDry
1 parent a1f3800 commit 1bfc5d6

File tree

8 files changed

+332
-328
lines changed

8 files changed

+332
-328
lines changed

examples/bible/configs.lua

Lines changed: 79 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,88 @@
1-
local Path = require("core.path")
2-
local utils = require("core.utils")
1+
local crazywall = require("crazywall")
2+
local utils = crazywall.utils
3+
local Path = crazywall.Path
34

4-
---@type table<string, PartialConfigTable>
5-
local configs = {
5+
local config = {
66

7-
bible = {
8-
note_schema = {
9-
{ "verses", "{{ ", " }}" },
10-
},
7+
note_schema = {
8+
{ "verses", "{{ ", " }}" },
9+
},
1110

12-
resolve_path = function()
13-
return Path.void()
14-
end,
11+
resolve_path = function()
12+
return Path.void()
13+
end,
1514

16-
transform_lines = function()
17-
return {}
18-
end,
15+
transform_lines = function()
16+
return {}
17+
end,
1918

20-
resolve_reference = function(section)
21-
local line = section:get_lines()[1]
22-
local book, chapter, starting_verse, ending_verse, version =
23-
line:match("(%d*%s*%w*) (%d+):(%d+)-(%d+) ?(%w*)")
24-
if not book then
25-
book, chapter, starting_verse, version = line:match("(%d*%s*%w*) (%d+):(%d+) ?(%w*)")
26-
ending_verse = starting_verse
27-
end
28-
if not book then
29-
--- Restore the section if it was unable to be parsed.
30-
return section:open_tag() .. section:get_lines() .. section:close_tag()
31-
end
32-
version = #version > 0 and version or "ASV"
33-
local check_jq = "(jq --version >/dev/null 2>/dev/null)"
34-
local jq_not_installed_msg = "echo 'Error: `jq` needs to be installed for this example.'"
35-
local curl_cmd = "(curl https://raw.githubusercontent.com/wldeh/bible-api/refs/heads/main/bibles/en-"
36-
.. version:lower()
37-
.. "/books/"
38-
.. book:lower():gsub(" ", "")
39-
.. "/chapters/"
40-
.. chapter
41-
.. ".json 2>/dev/null)"
42-
local jq_parse_json = "(jq -M -r '.data[] | select((.verse | tonumber) >= "
43-
.. starting_verse
44-
.. " and (.verse | tonumber) <= "
45-
.. ending_verse
46-
.. ") | .text' 2>/dev/null) | head -"
47-
.. (ending_verse - starting_verse + 1)
48-
.. ""
49-
local curl_cmd_failed = "echo 'Error: Curl command failed.'"
50-
local jq_cmd_failed = "echo 'Error: `jq` command failed.'"
51-
local cmd = string.format(
52-
"(%s || (%s && false)) && ((%s || (%s >&2 && false)) | (%s && true || %s)) 2>&1",
53-
check_jq,
54-
jq_not_installed_msg,
55-
curl_cmd,
56-
curl_cmd_failed,
57-
jq_parse_json,
58-
jq_cmd_failed
59-
)
60-
local handle = io.popen(cmd)
61-
local result = utils.read_from_handle(handle) or ""
62-
local output = "> **" .. line .. "**" .. "\n> "
63-
if utils.str.starts_with(result, "Error") or starting_verse == ending_verse then
64-
output = output .. result
65-
return output
66-
end
67-
local verse = starting_verse
68-
for verse_txt in utils.str.split_lines(result) do
69-
output = output .. "[" .. verse .. "] " .. verse_txt .. " "
70-
verse = verse + 1
71-
if verse > math.tointeger(ending_verse) then
72-
break
73-
end
74-
end
75-
output = output:sub(1, #output - 1)
19+
resolve_reference = function(section)
20+
local line = section:get_lines()[1]
21+
local book, chapter, starting_verse, ending_verse, version =
22+
line:match("(%d*%s*%w*) (%d+):(%d+)-(%d+) ?(%w*)")
23+
if not book then
24+
book, chapter, starting_verse, version =
25+
line:match("(%d*%s*%w*) (%d+):(%d+) ?(%w*)")
26+
ending_verse = starting_verse
27+
end
28+
if not book then
29+
--- Restore the section if it was unable to be parsed.
30+
return section:open_tag()
31+
.. section:get_lines()
32+
.. section:close_tag()
33+
end
34+
version = #version > 0 and version or "ASV"
35+
local check_jq = "(jq --version >/dev/null 2>/dev/null)"
36+
local jq_not_installed_msg =
37+
"echo 'Error: `jq` needs to be installed for this example.'"
38+
local curl_cmd = "(curl https://raw.githubusercontent.com/wldeh/bible-api/refs/heads/main/bibles/en-"
39+
.. version:lower()
40+
.. "/books/"
41+
.. book:lower():gsub(" ", "")
42+
.. "/chapters/"
43+
.. chapter
44+
.. ".json 2>/dev/null)"
45+
local jq_parse_json = "(jq -M -r '.data[] | select((.verse | tonumber) >= "
46+
.. starting_verse
47+
.. " and (.verse | tonumber) <= "
48+
.. ending_verse
49+
.. ") | .text' 2>/dev/null) | head -"
50+
.. (ending_verse - starting_verse + 1)
51+
.. ""
52+
local curl_cmd_failed = "echo 'Error: Curl command failed.'"
53+
local jq_cmd_failed = "echo 'Error: `jq` command failed.'"
54+
local cmd = string.format(
55+
"(%s || (%s && false)) && ((%s || (%s >&2 && false)) | (%s && true || %s)) 2>&1",
56+
check_jq,
57+
jq_not_installed_msg,
58+
curl_cmd,
59+
curl_cmd_failed,
60+
jq_parse_json,
61+
jq_cmd_failed
62+
)
63+
local handle = io.popen(cmd)
64+
local result = utils.read_from_handle(handle) or ""
65+
local output = "> **" .. line .. "**" .. "\n> "
66+
if
67+
utils.str.starts_with(result, "Error")
68+
or starting_verse == ending_verse
69+
then
70+
output = output .. result
7671
return output
77-
end,
72+
end
73+
local verse = starting_verse
74+
for verse_txt in utils.str.split_lines(result) do
75+
output = output .. "[" .. verse .. "] " .. verse_txt .. " "
76+
verse = verse + 1
77+
if verse > tonumber(ending_verse) then
78+
break
79+
end
80+
end
81+
output = output:sub(1, #output - 1)
82+
return output
83+
end,
7884

79-
allow_overwrite = true,
80-
},
85+
allow_overwrite = true,
8186
}
8287

83-
return configs
88+
crazywall.add_config("bible", config)

examples/bible/output/input.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414
> Jesus said to him, “I am the way, the truth, and the life. No one comes to the Father, except through me.
1515
1616
> **Acts 20:22-24 KJV**
17-
> [22] And now, behold, I go bound in the spirit unto Jerusalem, not knowing the things that shall befall me there: [23] Save that the Holy Ghost witnesseth in every city, saying that bonds and afflictions abide me. [24] But none of these things move me, neither count I my life dear unto myself, so that I might finish my course with joy, and the ministry, which I have received of the Lord Jesus, to testify the gospel of the grace of God.
17+
> [22] And now, behold, I go bound in the spirit unto Jerusalem, not knowing the things that shall befall me there: [23] Save that the Holy Ghost witnesseth in every city, saying that bonds and afflictions abide me. [24] But none of these things move me, neither count I my life dear unto myself, so that I might finish my course with joy, and the ministry, which I have received of the Lord Jesus, to testify the gospel of the grace of God.

examples/chess/configs.lua

Lines changed: 45 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,53 @@
1-
local Path = require("core.path")
1+
local crazywall = require("crazywall")
2+
local Path = crazywall.Path
23

3-
---@type table<string, PartialConfigTable>
4-
local configs = {
4+
local config = {
55

6-
chess = {
7-
note_schema = {
8-
{ "pgn", "```pgn", "```" },
9-
},
6+
note_schema = {
7+
{ "pgn", "```pgn", "```" },
8+
},
109

11-
resolve_path = function(section, ctx)
12-
local white, black, year, result
13-
for _, line in ipairs(section:get_lines()) do
14-
white = white or string.match(line, '^%[White "(.*)"%]')
15-
black = black or string.match(line, '^%[Black "(.*)"%]')
16-
year = year
17-
or string.match(line, '^%[Year "(.*)"%]')
18-
or string.match(line, '^%[Date "(%d%d%d%d)%..*"%]')
19-
result = result or string.match(line, '^%[Result "(.*)"%]')
20-
end
21-
if not (white and black and year and result) then
22-
return Path.void()
23-
end
24-
local filename = white
25-
.. " - "
26-
.. black
27-
.. " ("
28-
.. year
29-
.. ") "
30-
.. result
31-
.. ".pgn"
32-
return assert(ctx.dest_path:join(filename))
33-
end,
10+
resolve_path = function(section, ctx)
11+
local white, black, year, result
12+
for _, line in ipairs(section:get_lines()) do
13+
white = white or string.match(line, '^%[White "(.*)"%]')
14+
black = black or string.match(line, '^%[Black "(.*)"%]')
15+
year = year
16+
or string.match(line, '^%[Year "(.*)"%]')
17+
or string.match(line, '^%[Date "(%d%d%d%d)%..*"%]')
18+
result = result or string.match(line, '^%[Result "(.*)"%]')
19+
end
20+
if not (white and black and year and result) then
21+
return Path.void()
22+
end
23+
local filename = white
24+
.. " - "
25+
.. black
26+
.. " ("
27+
.. year
28+
.. ") "
29+
.. result
30+
.. ".pgn"
31+
return assert(ctx.dest_path:join(filename))
32+
end,
3433

35-
transform_lines = function(section)
36-
local lines = section:get_lines()
37-
table.remove(lines, 1)
38-
return lines
39-
end,
34+
transform_lines = function(section)
35+
local lines = section:get_lines()
36+
table.remove(lines, 1)
37+
return lines
38+
end,
4039

41-
resolve_reference = function(section)
42-
if section.path:is_void() then
43-
--- Restore the section if it was unable to be parsed.
44-
return section:open_tag()
45-
.. section:get_text()
46-
.. section:close_tag()
47-
end
48-
return "[[" .. section.path:get_filename():gsub("%.pgn", "") .. "]]"
49-
end,
40+
resolve_reference = function(section)
41+
if section.path:is_void() then
42+
--- Restore the section if it was unable to be parsed.
43+
return section:open_tag()
44+
.. section:get_text()
45+
.. section:close_tag()
46+
end
47+
return "[[" .. section.path:get_filename():gsub("%.pgn", "") .. "]]"
48+
end,
5049

51-
allow_overwrite = true,
52-
},
50+
allow_overwrite = true,
5351
}
5452

55-
return configs
53+
crazywall.add_config("chess", config)

examples/code/configs.lua

Lines changed: 55 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,62 @@
1-
local utils = require("core.utils")
2-
local Path = require("core.path")
1+
local crazywall = require("crazywall")
2+
local utils = crazywall.utils
3+
local Path = crazywall.Path
34

4-
---@type table<string, PartialConfigTable>
5-
local configs = {
6-
7-
code = {
8-
note_schema = {
9-
{ "py_write", "```py -> ", "```" },
10-
{ "py_read", "```py <- ", "```" },
11-
{ "execute", "```run", "```" },
12-
},
5+
local config = {
6+
7+
note_schema = {
8+
{ "py_write", "```py -> ", "```" },
9+
{ "py_read", "```py <- ", "```" },
10+
{ "execute", "```run", "```" },
11+
},
1312

14-
resolve_path = function(section, ctx)
15-
if section:type_name_is("py_write") then
16-
local desired_path = section:get_lines()[1]
17-
return assert(ctx.dest_path:join(desired_path))
18-
end
19-
return Path.void()
20-
end,
13+
resolve_path = function(section, ctx)
14+
if section:type_name_is("py_write") then
15+
local desired_path = section:get_lines()[1]
16+
return assert(ctx.dest_path:join(desired_path))
17+
end
18+
return Path.void()
19+
end,
2120

22-
transform_lines = function(section)
23-
local lines = section:get_lines()
24-
table.remove(lines, 1)
25-
return lines
26-
end,
21+
transform_lines = function(section)
22+
local lines = section:get_lines()
23+
table.remove(lines, 1)
24+
return lines
25+
end,
2726

28-
resolve_reference = function(section, ctx)
29-
if section:type_name_is("py_write") then
30-
return "[[" .. section:get_lines()[1] .. "]]"
31-
end
32-
if section:type_name_is("py_read") then
33-
local path_str = section:get_lines()[1]
34-
--- expand relative paths to be absolute paths
35-
local path = ctx.src_path:join(path_str)
36-
local handle = io.open(tostring(path), "r")
37-
local text = utils.read_from_handle(handle)
38-
return "```py <- " .. path_str .. "\n" .. text .. "\n```"
39-
end
40-
if section:type_name_is("execute") then
41-
assert(utils.str.starts_with(section:get_lines()[2], "$ "))
42-
local cmd = section:get_lines()[2]:sub(3)
43-
cmd = "cd "
44-
.. ctx.src_path:get_directory():escaped()
45-
.. "\n"
46-
.. cmd
47-
--- pipe stderr to stdout so that errors are visible
48-
.. " 2>&1"
49-
local handle = io.popen(cmd)
50-
local result = utils.read_from_handle(handle)
51-
return "```run\n"
52-
.. section:get_lines()[2]
53-
.. "\n"
54-
.. result
55-
.. "\n```"
56-
end
57-
error("unreachable")
58-
end,
27+
resolve_reference = function(section, ctx)
28+
if section:type_name_is("py_write") then
29+
return "[[" .. section:get_lines()[1] .. "]]"
30+
end
31+
if section:type_name_is("py_read") then
32+
local path_str = section:get_lines()[1]
33+
--- expand relative paths to be absolute paths
34+
local path = ctx.src_path:join(path_str)
35+
local handle = io.open(tostring(path), "r")
36+
local text = utils.read_from_handle(handle)
37+
return "```py <- " .. path_str .. "\n" .. text .. "\n```"
38+
end
39+
if section:type_name_is("execute") then
40+
assert(utils.str.starts_with(section:get_lines()[2], "$ "))
41+
local cmd = section:get_lines()[2]:sub(3)
42+
cmd = "cd "
43+
.. ctx.src_path:get_directory():escaped()
44+
.. "\n"
45+
.. cmd
46+
--- pipe stderr to stdout so that errors are visible
47+
.. " 2>&1"
48+
local handle = io.popen(cmd)
49+
local result = utils.read_from_handle(handle)
50+
return "```run\n"
51+
.. section:get_lines()[2]
52+
.. "\n"
53+
.. result
54+
.. "\n```"
55+
end
56+
error("unreachable")
57+
end,
5958

60-
allow_overwrite = true,
61-
},
59+
allow_overwrite = true,
6260
}
6361

64-
return configs
62+
crazywall.add_config("code", config)

0 commit comments

Comments
 (0)