Skip to content

Commit 5a9a5a5

Browse files
committed
Make 'begin' and 'end' markers case-insensitive. Make 'snip' commands not need a colon.
1 parent 453469d commit 5a9a5a5

File tree

7 files changed

+147
-44
lines changed

7 files changed

+147
-44
lines changed

Pipfile.lock

Lines changed: 104 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

source_document.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
import logging
77
from fuzzywuzzy import process
88

9-
SNIP_PREFIX="// snip:"
10-
SNIP_FILE_PREFIX="// snip-file:"
11-
TAG_PREFIX="// tag:"
9+
SNIP_PREFIX="// snip"
10+
SNIP_FILE_PREFIX="// snip-file"
11+
TAG_PREFIX="// tag"
1212

1313
# The virtual "ref" that represents the current state of the files on disk,
1414
# and may not necessarily be stored in the index or in a commit. Uses a
@@ -57,7 +57,7 @@ def find(base_path, extensions):
5757
@property
5858
def cleaned_contents(self):
5959
"""Returns a version of 'text' that has no expanded snippets."""
60-
snip_with_code = re.compile("(//.*snip(\-file)*:.*\n)(\[.*\]\n)*----\n(.*\n)*?----\n")
60+
snip_with_code = re.compile("(//.*snip(\-file)*:?.*\n)(\[.*\]\n)*----\n(.*\n)*?----\n", flags=re.IGNORECASE)
6161
cleaned = re.sub(snip_with_code, r'\1', self.contents)
6262
return cleaned
6363

@@ -80,19 +80,25 @@ def snippets(self):
8080
# encountered in the document
8181
current_ref = WORKSPACE_REF
8282

83+
tag_regex = re.compile(r"$\/\/\s*tag:?\s*(.*)^", flags=re.IGNORECASE)
84+
snip_regex = re.compile(r"$\/\/\s*tag:?\s*(.*)^", flags=re.IGNORECASE)
85+
8386
for line in source_lines:
8487
output_lines.append(line)
8588

8689
# change which tag we're looking at if we hit an instruction to
8790
# do so
88-
if line.startswith(TAG_PREFIX):
89-
current_ref = line[len(TAG_PREFIX)+1:].strip()
91+
tag = tag_regex.match(line)
92+
93+
if tag:
94+
current_ref = tag.groups(1).strip()
9095

9196
# is this a snippet?
92-
if line.startswith(SNIP_PREFIX):
97+
snippet = snip_regex.match(line)
98+
if snippet:
9399

94100
# figure out what tags we're supposed to be using here
95-
query_text = line[len(SNIP_PREFIX)+1:]
101+
query_text = snippet.groups(1)
96102

97103
# build the tag query from this
98104
query = TagQuery(query_text, ref=current_ref)

tagged_document.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ def parse_lines(self, data):
167167
# begin_re = re.compile(r"\s*(\/\/|#)\s*BEGIN\s+([^\s]+)")
168168
# end_re = re.compile(r"\s*(\/\/|#)\s*END\s+([^\s]+)")
169169

170-
begin_re = re.compile(r"\s*(\/\/|\#)\s*BEGIN\s+([^\s]+)")
171-
end_re = re.compile(r"\s*(\/\/|\#)\s*END\s+([^\s]+)")
170+
begin_re = re.compile(r"\s*(\/\/|\#)\s*BEGIN\s+([^\s]+)", flags=re.IGNORECASE)
171+
end_re = re.compile(r"\s*(\/\/|\#)\s*END\s+([^\s]+)", flags=re.IGNORECASE)
172172

173173

174174
current_tags = []
@@ -255,11 +255,11 @@ def __init__(self, query_string, ref="HEAD"):
255255
for token in tokens:
256256

257257
# Change mode if we have to
258-
if token == "except":
258+
if token.lower() == "except":
259259
mode = EXCLUDE_TAGS
260-
elif token == "highlighting":
260+
elif token.lower() == "highlighting":
261261
mode = HIGHLIGHT_TAGS
262-
elif token == "isolating":
262+
elif token.lower() == "isolating":
263263
mode = ISOLATE_TAGS
264264

265265
# Otherwise, add it to the list of tokens

tests/sample-expanded.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,20 @@ This is version 2 of source A.
2727
Finally, let's go back to the working copy.
2828

2929
// tag: working-copy
30-
// snip: sourceA
30+
// snip sourceA
3131
[source,swift]
3232
----
3333
This is version 4 of source A. This version of the file is not committed to the test repo.
3434
This line is tagged with both 'sourceA' and 'sourceA-1'.
3535
----
3636

3737
This is the final line of the example.
38+
39+
Let's check that python quotes work.
40+
41+
// snip python-quotes
42+
[source,swift]
43+
----
44+
This snippet uses python quotes, and is in lowercase.
45+
----
46+

tests/sample.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ Let's change to see the second tag.
1414
Finally, let's go back to the working copy.
1515

1616
// tag: working-copy
17-
// snip: sourceA
17+
// snip sourceA
1818

1919
This is the final line of the example.
20+
21+
Let's check that python quotes work.
22+
23+
// snip python-quotes
24+

tests/sourceB.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
// BEGIN sourceB
22
This file is not committed to the test repo.
3-
// END sourceB
3+
// END sourceB
4+
5+
# begin python-quotes
6+
This snippet uses python quotes, and is in lowercase.
7+
# end python-quotes
8+

tests/test_source_document.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
class SourceDocumentTests(unittest.TestCase):
77
"""Unit tests for the Document class"""
88
def test_cleaning(self):
9-
"""Tests removing snippets"""
9+
# Tests removing snippets
1010

1111
input_path = "tests/sample-expanded.txt"
1212
reference_path = "tests/sample.txt"
@@ -26,7 +26,7 @@ def test_finding_documents(self):
2626

2727

2828
def test_processing(self):
29-
"""Tests rendering a snippet using tagged documents."""
29+
# Tests rendering a snippet using tagged documents.
3030
repo = create_test_repo()
3131

3232
tagged_documents = TaggedDocument.find(repo, ["txt"])

0 commit comments

Comments
 (0)