Skip to content

improve handling of tokens ending with braces #185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions tests/data/jq.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# jq

> A command-line JSON processor that uses a domain-specific language.
> More information: <https://stedolan.github.io/jq>.

- Output a JSON file, in pretty-print format:

`jq . {{file.json}}`

- Output all elements from arrays (or all the values from objects) in a JSON file:

`jq '.[]' {{file.json}}`

- Read JSON objects from a file into an array, and output it (inverse of `jq .[]`):

`jq --slurp . {{file.json}}`

- Output the first element in a JSON file:

`jq '.[0]' {{file.json}}`

- Output the value of a given key of each element in a JSON text from stdin:

`cat {{file.json}} | jq 'map(.{{key_name}})'`

- Output the value of multiple keys as a new JSON object (assuming the input JSON has the keys `key_name` and `other_key_name`):

`cat {{file.json}} | jq '{{{my_new_key}}: .{{key_name}}, {{my_other_key}}: .{{other_key_name}}}'`

- Combine multiple filters:

`cat {{file.json}} | jq 'unique | sort | reverse'`

- Output the value of a given key to a string (and disable JSON output):

`cat {{file.json}} | jq --raw-output '"some text: \(.{{key_name}})"'`

30 changes: 30 additions & 0 deletions tests/data/jq_rendered
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

jq

A command-line JSON processor that uses a domain-specific language.
More information: https://stedolan.github.io/jq.

- Output a JSON file, in pretty-print format:
jq . file.json

- Output all elements from arrays (or all the values from objects) in a JSON file:
jq '.[]' file.json

- Read JSON objects from a file into an array, and output it (inverse of `jq .[]`):
jq --slurp . file.json

- Output the first element in a JSON file:
jq '.[0]' file.json

- Output the value of a given key of each element in a JSON text from stdin:
cat file.json | jq 'map(.key_name)'

- Output the value of multiple keys as a new JSON object (assuming the input JSON has the keys `key_name` and `other_key_name`):
cat file.json | jq '{my_new_key: .key_name, my_other_key: .other_key_name}'

- Combine multiple filters:
cat file.json | jq 'unique | sort | reverse'

- Output the value of a given key to a string (and disable JSON output):
cat file.json | jq --raw-output '"some text: \(.key_name)"'

16 changes: 11 additions & 5 deletions tests/test_tldr.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@
import types
from unittest import mock

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

def test_whole_page():
with open("tests/data/gem.md", "rb") as f_original:
with open("tests/data/gem_rendered", "rb") as f_rendered:

@pytest.mark.parametrize("page_name", page_names)
def test_whole_page(page_name):
with open(f"tests/data/{page_name}.md", "rb") as f_original:
with open(f"tests/data/{page_name}_rendered", "rb") as f_rendered:
old_stdout = sys.stdout
sys.stdout = io.StringIO()
sys.stdout.buffer = types.SimpleNamespace()
Expand All @@ -24,8 +29,9 @@ def test_whole_page():
assert tldr_output == correct_output


def test_markdown_mode():
with open("tests/data/gem.md", "rb") as f_original:
@pytest.mark.parametrize("page_name", page_names)
def test_markdown_mode(page_name):
with open(f"tests/data/{page_name}.md", "rb") as f_original:
d_original = f_original.read()
old_stdout = sys.stdout
sys.stdout = io.StringIO()
Expand Down
2 changes: 1 addition & 1 deletion tldr.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def get_page(

LEADING_SPACES_NUM = 2

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


Expand Down