Skip to content

Commit fd3c00a

Browse files
authored
Merge pull request #40 from cmacmackin/apply-black
Apply black
2 parents 993e858 + 490d3cd commit fd3c00a

File tree

4 files changed

+102
-45
lines changed

4 files changed

+102
-45
lines changed

.git-blame-ignore-revs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
b7b87a79b1bd34f6a74d16ea7dea57f454fb7926

.github/workflows/black.yaml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: black
2+
3+
on:
4+
push:
5+
paths:
6+
- '**.py'
7+
8+
defaults:
9+
run:
10+
shell: bash
11+
12+
jobs:
13+
black:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v3
17+
- name: Setup Python
18+
uses: actions/setup-python@v4
19+
with:
20+
python-version: 3.x
21+
- name: Install black
22+
run: |
23+
python -m pip install --upgrade pip
24+
pip install black
25+
- name: Version
26+
run: |
27+
python --version
28+
black --version
29+
- name: Run black
30+
run: |
31+
black markdown_include
32+
- uses: stefanzweifel/git-auto-commit-action@v4
33+
with:
34+
commit_message: "Apply black changes"

markdown_include/include.py

Lines changed: 65 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -29,55 +29,72 @@
2929
from markdown.extensions import Extension
3030
from markdown.preprocessors import Preprocessor
3131

32-
INC_SYNTAX = re.compile(r'{!\s*(.+?)\s*!((\blines\b)=([0-9 -]+))?\}')
33-
HEADING_SYNTAX = re.compile( '^#+' )
32+
INC_SYNTAX = re.compile(r"{!\s*(.+?)\s*!((\blines\b)=([0-9 -]+))?\}")
33+
HEADING_SYNTAX = re.compile("^#+")
3434

3535

3636
class MarkdownInclude(Extension):
3737
def __init__(self, configs={}):
3838
self.config = {
39-
'base_path': ['.', 'Default location from which to evaluate ' \
40-
'relative paths for the include statement.'],
41-
'encoding': ['utf-8', 'Encoding of the files used by the include ' \
42-
'statement.'],
43-
'inheritHeadingDepth': [False, 'Increases headings on included ' \
44-
'file by amount of previous heading (combines with '\
45-
'headingOffset option).'],
46-
'headingOffset': [0, 'Increases heading depth by a specific ' \
47-
'amount (and the inheritHeadingDepth option). Defaults to 0.'],
48-
'throwException': [False, 'When true, if the extension is unable '\
49-
'to find an included file it will throw an '\
50-
'exception which the user can catch. If false '\
51-
'(default), a warning will be printed and '\
52-
'Markdown will continue parsing the file.']
39+
"base_path": [
40+
".",
41+
"Default location from which to evaluate "
42+
"relative paths for the include statement.",
43+
],
44+
"encoding": [
45+
"utf-8",
46+
"Encoding of the files used by the include " "statement.",
47+
],
48+
"inheritHeadingDepth": [
49+
False,
50+
"Increases headings on included "
51+
"file by amount of previous heading (combines with "
52+
"headingOffset option).",
53+
],
54+
"headingOffset": [
55+
0,
56+
"Increases heading depth by a specific "
57+
"amount (and the inheritHeadingDepth option). Defaults to 0.",
58+
],
59+
"throwException": [
60+
False,
61+
"When true, if the extension is unable "
62+
"to find an included file it will throw an "
63+
"exception which the user can catch. If false "
64+
"(default), a warning will be printed and "
65+
"Markdown will continue parsing the file.",
66+
],
5367
}
5468
for key, value in configs.items():
5569
self.setConfig(key, value)
5670

5771
def extendMarkdown(self, md):
58-
md.preprocessors.register(IncludePreprocessor(md,self.getConfigs()), 'include', 101)
72+
md.preprocessors.register(
73+
IncludePreprocessor(md, self.getConfigs()), "include", 101
74+
)
5975

6076

6177
class IncludePreprocessor(Preprocessor):
62-
'''
78+
"""
6379
This provides an "include" function for Markdown, similar to that found in
6480
LaTeX (also the C pre-processor and Fortran). The syntax is {!filename!},
6581
which will be replaced by the contents of filename. Any such statements in
6682
filename will also be replaced. This replacement is done prior to any other
6783
Markdown processing. All file-names are evaluated relative to the location
6884
from which Markdown is being called.
69-
'''
85+
"""
86+
7087
def __init__(self, md, config):
7188
super(IncludePreprocessor, self).__init__(md)
72-
self.base_path = config['base_path']
73-
self.encoding = config['encoding']
74-
self.inheritHeadingDepth = config['inheritHeadingDepth']
75-
self.headingOffset = config['headingOffset']
76-
self.throwException = config['throwException']
89+
self.base_path = config["base_path"]
90+
self.encoding = config["encoding"]
91+
self.inheritHeadingDepth = config["inheritHeadingDepth"]
92+
self.headingOffset = config["headingOffset"]
93+
self.throwException = config["throwException"]
7794

7895
def run(self, lines):
7996
done = False
80-
bonusHeading = ''
97+
bonusHeading = ""
8198
while not done:
8299
for loc, line in enumerate(lines):
83100
m = INC_SYNTAX.search(line)
@@ -87,17 +104,19 @@ def run(self, lines):
87104
filename = os.path.expanduser(filename)
88105
if not os.path.isabs(filename):
89106
filename = os.path.normpath(
90-
os.path.join(self.base_path,filename)
107+
os.path.join(self.base_path, filename)
91108
)
92109
try:
93-
with open(filename, 'r', encoding=self.encoding) as r:
110+
with open(filename, "r", encoding=self.encoding) as r:
94111
original_text = self.run(r.readlines())
95-
112+
96113
except Exception as e:
97114
if not self.throwException:
98-
print('Warning: could not find file {}. Ignoring '
99-
'include statement. Error: {}'.format(filename, e))
100-
lines[loc] = INC_SYNTAX.sub('',line)
115+
print(
116+
"Warning: could not find file {}. Ignoring "
117+
"include statement. Error: {}".format(filename, e)
118+
)
119+
lines[loc] = INC_SYNTAX.sub("", line)
101120
break
102121
else:
103122
raise e
@@ -127,8 +146,10 @@ def run(self, lines):
127146
f"smaller than the end line: {current_end} "
128147
f"using start: {current_start}"
129148
)
130-
131-
wanted_lines.extend(original_text[current_start-1:current_end])
149+
150+
wanted_lines.extend(
151+
original_text[current_start - 1 : current_end]
152+
)
132153
else:
133154
wanted_line = int(block.strip())
134155
current_line = wanted_line
@@ -138,39 +159,38 @@ def run(self, lines):
138159
f"Warning: line: {wanted_line} is larger than "
139160
f"file: {filename} using end: {current_line}"
140161
)
141-
wanted_lines.append(original_text[current_line-1])
162+
wanted_lines.append(original_text[current_line - 1])
142163
text = wanted_lines
143164

144-
145165
if len(text) == 0:
146-
text.append('')
166+
text.append("")
147167
for i in range(len(text)):
148168
# Strip the newline, and optionally increase header depth
149169
if self.inheritHeadingDepth or self.headingOffset:
150170
if HEADING_SYNTAX.search(text[i]):
151-
text[i] = text[i].rstrip('\r\n')
171+
text[i] = text[i].rstrip("\r\n")
152172
if self.inheritHeadingDepth:
153173
text[i] = bonusHeading + text[i]
154174
if self.headingOffset:
155-
text[i] = '#' * self.headingOffset + text[i]
175+
text[i] = "#" * self.headingOffset + text[i]
156176
else:
157-
text[i] = text[i].rstrip('\r\n')
158-
text_to_insert = '\r\n'.join(text)
159-
line = line[:m.start()] + text_to_insert.strip() + line[m.end():]
177+
text[i] = text[i].rstrip("\r\n")
178+
text_to_insert = "\r\n".join(text)
179+
line = line[: m.start()] + text_to_insert.strip() + line[m.end() :]
160180
del lines[loc]
161-
lines[loc:loc] = line.split('\r\n')
181+
lines[loc:loc] = line.split("\r\n")
162182
m = INC_SYNTAX.search(line)
163183

164184
else:
165185
h = HEADING_SYNTAX.search(line)
166186
if h:
167187
headingDepth = len(h.group(0))
168-
bonusHeading = '#' * headingDepth
169-
188+
bonusHeading = "#" * headingDepth
189+
170190
else:
171191
done = True
172192
return lines
173193

174194

175-
def makeExtension(*args,**kwargs):
195+
def makeExtension(*args, **kwargs):
176196
return MarkdownInclude(kwargs)

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,5 @@ packages = ["markdown_include"]
4040

4141
[tool.setuptools.dynamic]
4242
version = { attr = "setuptools_scm.get_version" }
43+
44+
[tool.black]

0 commit comments

Comments
 (0)