Skip to content

Commit

Permalink
Merge pull request #40 from cmacmackin/apply-black
Browse files Browse the repository at this point in the history
Apply black
  • Loading branch information
ZedThree authored Nov 23, 2022
2 parents 993e858 + 490d3cd commit fd3c00a
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 45 deletions.
1 change: 1 addition & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
b7b87a79b1bd34f6a74d16ea7dea57f454fb7926
34 changes: 34 additions & 0 deletions .github/workflows/black.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: black

on:
push:
paths:
- '**.py'

defaults:
run:
shell: bash

jobs:
black:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: 3.x
- name: Install black
run: |
python -m pip install --upgrade pip
pip install black
- name: Version
run: |
python --version
black --version
- name: Run black
run: |
black markdown_include
- uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: "Apply black changes"
110 changes: 65 additions & 45 deletions markdown_include/include.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,55 +29,72 @@
from markdown.extensions import Extension
from markdown.preprocessors import Preprocessor

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


class MarkdownInclude(Extension):
def __init__(self, configs={}):
self.config = {
'base_path': ['.', 'Default location from which to evaluate ' \
'relative paths for the include statement.'],
'encoding': ['utf-8', 'Encoding of the files used by the include ' \
'statement.'],
'inheritHeadingDepth': [False, 'Increases headings on included ' \
'file by amount of previous heading (combines with '\
'headingOffset option).'],
'headingOffset': [0, 'Increases heading depth by a specific ' \
'amount (and the inheritHeadingDepth option). Defaults to 0.'],
'throwException': [False, 'When true, if the extension is unable '\
'to find an included file it will throw an '\
'exception which the user can catch. If false '\
'(default), a warning will be printed and '\
'Markdown will continue parsing the file.']
"base_path": [
".",
"Default location from which to evaluate "
"relative paths for the include statement.",
],
"encoding": [
"utf-8",
"Encoding of the files used by the include " "statement.",
],
"inheritHeadingDepth": [
False,
"Increases headings on included "
"file by amount of previous heading (combines with "
"headingOffset option).",
],
"headingOffset": [
0,
"Increases heading depth by a specific "
"amount (and the inheritHeadingDepth option). Defaults to 0.",
],
"throwException": [
False,
"When true, if the extension is unable "
"to find an included file it will throw an "
"exception which the user can catch. If false "
"(default), a warning will be printed and "
"Markdown will continue parsing the file.",
],
}
for key, value in configs.items():
self.setConfig(key, value)

def extendMarkdown(self, md):
md.preprocessors.register(IncludePreprocessor(md,self.getConfigs()), 'include', 101)
md.preprocessors.register(
IncludePreprocessor(md, self.getConfigs()), "include", 101
)


class IncludePreprocessor(Preprocessor):
'''
"""
This provides an "include" function for Markdown, similar to that found in
LaTeX (also the C pre-processor and Fortran). The syntax is {!filename!},
which will be replaced by the contents of filename. Any such statements in
filename will also be replaced. This replacement is done prior to any other
Markdown processing. All file-names are evaluated relative to the location
from which Markdown is being called.
'''
"""

def __init__(self, md, config):
super(IncludePreprocessor, self).__init__(md)
self.base_path = config['base_path']
self.encoding = config['encoding']
self.inheritHeadingDepth = config['inheritHeadingDepth']
self.headingOffset = config['headingOffset']
self.throwException = config['throwException']
self.base_path = config["base_path"]
self.encoding = config["encoding"]
self.inheritHeadingDepth = config["inheritHeadingDepth"]
self.headingOffset = config["headingOffset"]
self.throwException = config["throwException"]

def run(self, lines):
done = False
bonusHeading = ''
bonusHeading = ""
while not done:
for loc, line in enumerate(lines):
m = INC_SYNTAX.search(line)
Expand All @@ -87,17 +104,19 @@ def run(self, lines):
filename = os.path.expanduser(filename)
if not os.path.isabs(filename):
filename = os.path.normpath(
os.path.join(self.base_path,filename)
os.path.join(self.base_path, filename)
)
try:
with open(filename, 'r', encoding=self.encoding) as r:
with open(filename, "r", encoding=self.encoding) as r:
original_text = self.run(r.readlines())

except Exception as e:
if not self.throwException:
print('Warning: could not find file {}. Ignoring '
'include statement. Error: {}'.format(filename, e))
lines[loc] = INC_SYNTAX.sub('',line)
print(
"Warning: could not find file {}. Ignoring "
"include statement. Error: {}".format(filename, e)
)
lines[loc] = INC_SYNTAX.sub("", line)
break
else:
raise e
Expand Down Expand Up @@ -127,8 +146,10 @@ def run(self, lines):
f"smaller than the end line: {current_end} "
f"using start: {current_start}"
)

wanted_lines.extend(original_text[current_start-1:current_end])

wanted_lines.extend(
original_text[current_start - 1 : current_end]
)
else:
wanted_line = int(block.strip())
current_line = wanted_line
Expand All @@ -138,39 +159,38 @@ def run(self, lines):
f"Warning: line: {wanted_line} is larger than "
f"file: {filename} using end: {current_line}"
)
wanted_lines.append(original_text[current_line-1])
wanted_lines.append(original_text[current_line - 1])
text = wanted_lines


if len(text) == 0:
text.append('')
text.append("")
for i in range(len(text)):
# Strip the newline, and optionally increase header depth
if self.inheritHeadingDepth or self.headingOffset:
if HEADING_SYNTAX.search(text[i]):
text[i] = text[i].rstrip('\r\n')
text[i] = text[i].rstrip("\r\n")
if self.inheritHeadingDepth:
text[i] = bonusHeading + text[i]
if self.headingOffset:
text[i] = '#' * self.headingOffset + text[i]
text[i] = "#" * self.headingOffset + text[i]
else:
text[i] = text[i].rstrip('\r\n')
text_to_insert = '\r\n'.join(text)
line = line[:m.start()] + text_to_insert.strip() + line[m.end():]
text[i] = text[i].rstrip("\r\n")
text_to_insert = "\r\n".join(text)
line = line[: m.start()] + text_to_insert.strip() + line[m.end() :]
del lines[loc]
lines[loc:loc] = line.split('\r\n')
lines[loc:loc] = line.split("\r\n")
m = INC_SYNTAX.search(line)

else:
h = HEADING_SYNTAX.search(line)
if h:
headingDepth = len(h.group(0))
bonusHeading = '#' * headingDepth
bonusHeading = "#" * headingDepth

else:
done = True
return lines


def makeExtension(*args,**kwargs):
def makeExtension(*args, **kwargs):
return MarkdownInclude(kwargs)
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,5 @@ packages = ["markdown_include"]

[tool.setuptools.dynamic]
version = { attr = "setuptools_scm.get_version" }

[tool.black]

0 comments on commit fd3c00a

Please sign in to comment.