Skip to content

Commit 6ba64ff

Browse files
committed
Add spairs() to the logging namespace, and update docs accordingly
Also fix some bugs: 1. Improve logic to ignore functions in pandoc objects (they were confusing attribute-counting logic). 2. Fix bug that affected reporting in (rare) cases where objects have both numeric and non-numeric keys. 3. Avoid passing values to string.format(). This was previously done with the %q formatting string, but this didn't work with multi-byte characters.
1 parent dd7f888 commit 6ba64ff

File tree

9 files changed

+467
-30
lines changed

9 files changed

+467
-30
lines changed

README.md

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ text
2727
blocks: Blocks {
2828
[1] Para {
2929
content: Inlines {
30-
[1] Str text: "text"
30+
[1] Str "text"
3131
}
3232
}
3333
}
@@ -60,7 +60,7 @@ end
6060
```text
6161
% pandoc simple.md -L para.lua >/dev/null --verbose
6262
[INFO] Running filter para.lua
63-
(I) para Para {, content: Inlines {[1] Str text: "text"}}
63+
(I) para Para {content: Inlines {[1] Str "text"}}
6464
[INFO] Completed filter para.lua in 8 ms
6565
```
6666

@@ -89,13 +89,15 @@ end
8989
```text
9090
% pandoc simple.md -L para2.lua >/dev/null --verbose
9191
[INFO] Running filter para2.lua
92-
(I) para Para {, content: Inlines {[1] Str text: "text"}}
93-
(I) para.content Inlines {[1] Str text: "text"}
94-
(I) para.content[1] Str text: "text"
92+
(I) para Para {content: Inlines {[1] Str "text"}}
93+
(I) para.content Inlines {[1] Str "text"}
94+
(I) para.content[1] Str "text"
9595
(I) para.content[1].text text
9696
[INFO] Completed filter para2.lua in 8 ms
9797
```
9898

99+
Why is the last `text` not quoted?
100+
99101
# Module contents
100102

101103
## logging.type(value)
@@ -105,6 +107,12 @@ Returns whatever [`pandoc.utils.type()`](https://pandoc.org/lua-filters.html#pan
105107
* Spaces are replaced with periods, e.g., `pandoc Row` becomes `pandoc.Row`
106108
* `Inline` and `Block` are replaced with the corresponding `tag` value, e.g. `Emph` or `Table`
107109

110+
## logging.spairs(list [, comp])
111+
112+
Like `pairs()` but with sorted keys. Keys are converted to strings and sorted
113+
using `table.sort(keys, comp)` so by default they're visited in alphabetical
114+
order.
115+
108116
## logging.dump(value [, maxlen])
109117

110118
Returns a pandoc-aware string representation of `value`, which can be an arbitrary lua object.
@@ -113,15 +121,16 @@ The returned string is a single line if not longer than `maxlen` (default `70`),
113121

114122
Map keys are sorted alphabetically in order to ensure that output is repeatable.
115123

116-
117-
118-
See the *Getting started* section for examples, and note that
124+
See the *Getting started* section for examples.
119125

120126
## logging.output(...)
121127

122128
Pass each argument to `logging.dump()` and output the results to `stderr`, separated by single spaces and terminated (if necessary) with a newline.
123129

124-
Note: Actually (as a slight optimization) only `table` and `userdata` arguments are passed to `logging.dump()`. Other arguments are passed to the built-in `tostring()` function.
130+
Note: Only `table` and `userdata` arguments are passed to
131+
`logging.dump()`. Other arguments are passed to the built-in `tostring()`
132+
function. This is partly an optimization and partly to prevent string arguments
133+
from being quoted.
125134

126135
## logging.loglevel
127136

examples/Makefile

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@ DIFF ?= diff --strip-trailing-cr --unified
22
LUA ?= lua
33
PANDOC ?= pandoc
44

5+
PANDOCFLAGS +=
6+
57
STANDALONE = dumpex.lua levels.lua
68
FILTER = para.lua para2.lua simple.lua table.lua
79

10+
test-filter-para.lua test-filter-para2.lua: \
11+
PANDOCFLAGS += --verbose
12+
813
# make COMPARE= to run tools without redirection or comparison
914
# make SNAPSHOT=true to create .expected files
15+
FIX = sed -e 's/in [0-9]* ms/in NN ms/'
1016
ifneq "$(SNAPSHOT)" ""
11-
COMPARE = 2>$*.expected >/dev/null
17+
COMPARE = 2>&1 >/dev/null | $(FIX) >$*.expected
1218
else
13-
COMPARE = 2>&1 >/dev/null | $(DIFF) $*.expected -
19+
COMPARE = 2>&1 >/dev/null | $(FIX) | $(DIFF) $*.expected -
1420
endif
1521

1622
test: test-standalone test-filter
@@ -23,4 +29,4 @@ test-standalone-%: %
2329
$(LUA) $* $(COMPARE)
2430

2531
test-filter-%: %
26-
$(PANDOC) $(wildcard *.md) -L $* $(COMPARE)
32+
$(PANDOC) $(PANDOCFLAGS) $(wildcard *.md) -L $* $(COMPARE)

examples/dumpex.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ output(logging.dump(nil))
88
output(logging.dump(1))
99
output(logging.dump(false))
1010
output(logging.dump('string'))
11+
output(logging.dump('Here’s a non-ASCII character.'))
1112
output(logging.dump({}))
1213
output(logging.dump({1, 2, 3}))
1314
output(logging.dump({a=1, b=2, c=3}))

examples/dumpex.lua.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ nil
22
1
33
false
44
"string"
5+
"Here’s a non-ASCII character."
56
{}
67
{[1] 1, [2] 2, [3] 3}
78
{a: 1, b: 2, c: 3}

examples/para.lua.expected

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
[INFO] Running filter para.lua
2+
(I) para Para {
3+
content: Inlines {
4+
[1] Str "This"
5+
[2] Space
6+
[3] Str "directory"
7+
[4] Space
8+
[5] Str "contains"
9+
[6] Space
10+
[7] Str "example"
11+
[8] Space
12+
[9] Str "lua"
13+
[10] Space
14+
[11] Str "files"
15+
[12] Space
16+
[13] Str "(standalone"
17+
[14] Space
18+
[15] Str "and"
19+
[16] Space
20+
[17] Str "pandoc"
21+
[18] Space
22+
[19] Str "filters)"
23+
[20] Space
24+
[21] Str "and"
25+
[22] Space
26+
[23] Str "some"
27+
[24] Space
28+
[25] Str "sample"
29+
[26] Space
30+
[27] Str "markdown"
31+
[28] Space
32+
[29] Str "files."
33+
}
34+
}
35+
(I) para Para {
36+
content: Inlines {
37+
[1] Str "These"
38+
[2] Space
39+
[3] Str "files"
40+
[4] Space
41+
[5] Str "were"
42+
[6] Space
43+
[7] Str "used"
44+
[8] Space
45+
[9] Str "when"
46+
[10] Space
47+
[11] Str "generating"
48+
[12] Space
49+
[13] Str "the"
50+
[14] Space
51+
[15] Str "examples"
52+
[16] Space
53+
[17] Str "in"
54+
[18] Space
55+
[19] Str "the"
56+
[20] Space
57+
[21] Str "top-level"
58+
[22] Space
59+
[23] Str "README"
60+
[24] Space
61+
[25] Str "file."
62+
}
63+
}
64+
(I) para Para {
65+
content: Inlines {
66+
[1] Str "There’s"
67+
[2] Space
68+
[3] Str "also"
69+
[4] Space
70+
[5] Str "a"
71+
[6] Space
72+
[7] Code {
73+
attr: Attr {
74+
attributes: AttributeList {}
75+
classes: List {}
76+
identifier: ""
77+
}
78+
text: "Makefile"
79+
}
80+
[8] Space
81+
[9] Str "that"
82+
[10] Space
83+
[11] Str "runs"
84+
[12] Space
85+
[13] Str "lua"
86+
[14] Space
87+
[15] Str "and"
88+
[16] Space
89+
[17] Str "pandoc"
90+
[18] Space
91+
[19] Str "(as"
92+
[20] Space
93+
[21] Str "appropriate)"
94+
[22] Space
95+
[23] Str "and"
96+
[24] Space
97+
[25] Str "compares"
98+
[26] Space
99+
[27] Str "the"
100+
[28] Space
101+
[29] Str "output"
102+
[30] Space
103+
[31] Str "with"
104+
[32] Space
105+
[33] Str "the"
106+
[34] Space
107+
[35] Str "expected"
108+
[36] Space
109+
[37] Str "output."
110+
}
111+
}
112+
(I) para Para {
113+
content: Inlines {
114+
[1] Str "Paragraph"
115+
[2] Space
116+
[3] Str "with"
117+
[4] Space
118+
[5] Emph {
119+
content: Inlines {
120+
[1] Str "emphasized"
121+
}
122+
}
123+
[6] Space
124+
[7] Str "and"
125+
[8] Space
126+
[9] Strong {
127+
content: Inlines {
128+
[1] Str "bold"
129+
}
130+
}
131+
[10] Space
132+
[11] Str "text."
133+
}
134+
}
135+
(I) para Para {content: Inlines {[1] Str "text"}}
136+
(I) para Para {content: Inlines {[1] Str "smart-quote-’"}}
137+
[INFO] Completed filter para.lua in NN ms

0 commit comments

Comments
 (0)