Skip to content

Commit f17976a

Browse files
authored
improve handling of tokens ending with braces (#185)
1 parent b2d031c commit f17976a

File tree

4 files changed

+79
-6
lines changed

4 files changed

+79
-6
lines changed

tests/data/jq.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# jq
2+
3+
> A command-line JSON processor that uses a domain-specific language.
4+
> More information: <https://stedolan.github.io/jq>.
5+
6+
- Output a JSON file, in pretty-print format:
7+
8+
`jq . {{file.json}}`
9+
10+
- Output all elements from arrays (or all the values from objects) in a JSON file:
11+
12+
`jq '.[]' {{file.json}}`
13+
14+
- Read JSON objects from a file into an array, and output it (inverse of `jq .[]`):
15+
16+
`jq --slurp . {{file.json}}`
17+
18+
- Output the first element in a JSON file:
19+
20+
`jq '.[0]' {{file.json}}`
21+
22+
- Output the value of a given key of each element in a JSON text from stdin:
23+
24+
`cat {{file.json}} | jq 'map(.{{key_name}})'`
25+
26+
- Output the value of multiple keys as a new JSON object (assuming the input JSON has the keys `key_name` and `other_key_name`):
27+
28+
`cat {{file.json}} | jq '{{{my_new_key}}: .{{key_name}}, {{my_other_key}}: .{{other_key_name}}}'`
29+
30+
- Combine multiple filters:
31+
32+
`cat {{file.json}} | jq 'unique | sort | reverse'`
33+
34+
- Output the value of a given key to a string (and disable JSON output):
35+
36+
`cat {{file.json}} | jq --raw-output '"some text: \(.{{key_name}})"'`
37+

tests/data/jq_rendered

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
jq
3+
4+
A command-line JSON processor that uses a domain-specific language.
5+
More information: https://stedolan.github.io/jq.
6+
7+
- Output a JSON file, in pretty-print format:
8+
jq . file.json
9+
10+
- Output all elements from arrays (or all the values from objects) in a JSON file:
11+
jq '.[]' file.json
12+
13+
- Read JSON objects from a file into an array, and output it (inverse of `jq .[]`):
14+
jq --slurp . file.json
15+
16+
- Output the first element in a JSON file:
17+
jq '.[0]' file.json
18+
19+
- Output the value of a given key of each element in a JSON text from stdin:
20+
cat file.json | jq 'map(.key_name)'
21+
22+
- Output the value of multiple keys as a new JSON object (assuming the input JSON has the keys `key_name` and `other_key_name`):
23+
cat file.json | jq '{my_new_key: .key_name, my_other_key: .other_key_name}'
24+
25+
- Combine multiple filters:
26+
cat file.json | jq 'unique | sort | reverse'
27+
28+
- Output the value of a given key to a string (and disable JSON output):
29+
cat file.json | jq --raw-output '"some text: \(.key_name)"'
30+

tests/test_tldr.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@
66
import types
77
from unittest import mock
88

9+
# gem is a basic test of page rendering
10+
# jq is a more complicated test for token parsing
11+
page_names = ('gem', 'jq')
912

10-
def test_whole_page():
11-
with open("tests/data/gem.md", "rb") as f_original:
12-
with open("tests/data/gem_rendered", "rb") as f_rendered:
13+
14+
@pytest.mark.parametrize("page_name", page_names)
15+
def test_whole_page(page_name):
16+
with open(f"tests/data/{page_name}.md", "rb") as f_original:
17+
with open(f"tests/data/{page_name}_rendered", "rb") as f_rendered:
1318
old_stdout = sys.stdout
1419
sys.stdout = io.StringIO()
1520
sys.stdout.buffer = types.SimpleNamespace()
@@ -24,8 +29,9 @@ def test_whole_page():
2429
assert tldr_output == correct_output
2530

2631

27-
def test_markdown_mode():
28-
with open("tests/data/gem.md", "rb") as f_original:
32+
@pytest.mark.parametrize("page_name", page_names)
33+
def test_markdown_mode(page_name):
34+
with open(f"tests/data/{page_name}.md", "rb") as f_original:
2935
d_original = f_original.read()
3036
old_stdout = sys.stdout
3137
sys.stdout = io.StringIO()

tldr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ def get_page(
296296

297297
LEADING_SPACES_NUM = 2
298298

299-
COMMAND_SPLIT_REGEX = re.compile(r'(?P<param>{{.+?}})')
299+
COMMAND_SPLIT_REGEX = re.compile(r'(?P<param>{{.+?}*}})')
300300
PARAM_REGEX = re.compile(r'(?:{{)(?P<param>.+?)(?:}})')
301301

302302

0 commit comments

Comments
 (0)