Skip to content

Commit

Permalink
Add inside_block support
Browse files Browse the repository at this point in the history
  • Loading branch information
rnorth committed Oct 5, 2018
1 parent 285ec56 commit fb39213
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
9 changes: 9 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM python:3.6.6-slim

RUN pip install mkdocs-material

COPY . /src
RUN pip install -e /src
EXPOSE 8000

CMD exec mkdocs serve --dev-addr 0.0.0.0:8000
8 changes: 5 additions & 3 deletions codeinclude/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
$
'''

def get_substitute(page, title, filename, lines, block):
def get_substitute(page, title, filename, lines, block, inside_block):

page_parent_dir = os.path.dirname(page.file.abs_src_path)
import_path = os.path.join(page_parent_dir, filename)
with open(import_path) as f:
content = f.read()

selected_content = select(content, lines=lines, block=block)
selected_content = select(content, lines=lines, block=block, inside_block=inside_block)

return "\n```java tab=\"" + title + "\"\n" + selected_content + "\n```\n\n"

Expand Down Expand Up @@ -65,8 +65,10 @@ def on_page_markdown(self, markdown, page, config,
params = dict(token.split(":") for token in shlex.split(raw_params))
lines = params.get("lines", "")
block = params.get("block", "")
inside_block = params.get("inside_block", "")

code_block = get_substitute(page, title, filename, lines, block)

code_block = get_substitute(page, title, filename, lines, block, inside_block)
# re-indent
code_block = re.sub("^", indent, code_block, flags=re.MULTILINE)
results += code_block
Expand Down
21 changes: 19 additions & 2 deletions codeinclude/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
def select(text,
lines=None,
from_token=None, to_token=None,
block=None,
block=None,
inside_block=None,
lang=None):

selected_lines = []
Expand Down Expand Up @@ -34,6 +35,22 @@ def select(text,

delim_count -= line.count("}")

if inside_block:
i = 0
delim_count = 0
for line in text.splitlines():
first_line_of_block = False
i = i + 1
if inside_block in line and delim_count <= 0:
delim_count = 0
delim_count += line.count("{")
first_line_of_block = True

delim_count -= line.count("}")

if delim_count > 0 and not first_line_of_block:
selected_lines.append(i)

if from_token and to_token:
i = 0
active = False
Expand All @@ -53,7 +70,7 @@ def select(text,

last_selected = 0
for i in sorted(selected_lines):
if i > (last_selected + 1):
if i > (last_selected + 1) and last_selected != 0:
result += "\n\n\n"
result += source_lines[i - 1] + "\n"
last_selected = i
Expand Down

0 comments on commit fb39213

Please sign in to comment.