Added {% include ... %} directive for including examples/travis-ci-minimal.yml in docs/setup.md
This commit is contained in:
@@ -1,6 +1,19 @@
|
||||
import mkdocs, re, os, io, cgi
|
||||
|
||||
TAG_REGEX_PATTERN = re.compile(
|
||||
INCLUDE_TAG_REGEX = re.compile(
|
||||
r'''
|
||||
{% # opening tag
|
||||
\s*
|
||||
include # directive name
|
||||
\s+
|
||||
"(?P<filename>[^"]+)" # "filename"
|
||||
\s*
|
||||
%} # closing tag
|
||||
''',
|
||||
flags=re.VERBOSE,
|
||||
)
|
||||
|
||||
INCLUDEMARKDOWN_TAG_REGEX = re.compile(
|
||||
r'''
|
||||
{% # opening tag
|
||||
\s*
|
||||
@@ -9,8 +22,6 @@ TAG_REGEX_PATTERN = re.compile(
|
||||
"(?P<filename>[^"]+)" # "filename"
|
||||
(?:\s+start="(?P<start>[^"]+)")? # optional start expression
|
||||
(?:\s+end="(?P<end>[^"]+)")? # optional end expression
|
||||
(?:\s+before="(?P<before>[^"]+)")? # optional preceding text to add
|
||||
(?:\s+after="(?P<after>[^"]+)")? # optional succeeding text to add
|
||||
\s*
|
||||
%} # closing tag
|
||||
''',
|
||||
@@ -21,12 +32,24 @@ class ImportMarkdownPlugin(mkdocs.plugins.BasePlugin):
|
||||
def on_page_markdown(self, markdown, page, **kwargs):
|
||||
page_src_path = page.file.abs_src_path
|
||||
|
||||
def found_import_markdown_tag(match):
|
||||
def found_include_tag(match):
|
||||
filename = match.group('filename')
|
||||
|
||||
file_path_abs = os.path.join(os.path.dirname(page_src_path), filename)
|
||||
|
||||
if not os.path.exists(file_path_abs):
|
||||
raise ValueError('file not found', filename)
|
||||
|
||||
with io.open(file_path_abs, encoding='utf8') as f:
|
||||
text_to_include = f.read()
|
||||
|
||||
return text_to_include
|
||||
|
||||
|
||||
def found_includemarkdown_tag(match):
|
||||
filename = match.group('filename')
|
||||
start = match.group('start')
|
||||
end = match.group('end')
|
||||
before = match.group('before')
|
||||
after = match.group('after')
|
||||
|
||||
file_path_abs = os.path.join(os.path.dirname(page_src_path), filename)
|
||||
|
||||
@@ -42,12 +65,6 @@ class ImportMarkdownPlugin(mkdocs.plugins.BasePlugin):
|
||||
if end:
|
||||
text_to_include, _, _ = text_to_include.partition(end)
|
||||
|
||||
if before:
|
||||
text_to_include = before.replace('\\n', '\n') + text_to_include
|
||||
|
||||
if after:
|
||||
text_to_include = text_to_include + after.replace('\\n', '\n')
|
||||
|
||||
return (
|
||||
'<!-- BEGIN INCLUDE %s %s %s -->\n' % (
|
||||
filename, cgi.escape(start or ''), cgi.escape(end or '')
|
||||
@@ -56,6 +73,7 @@ class ImportMarkdownPlugin(mkdocs.plugins.BasePlugin):
|
||||
+ '\n<!-- END INCLUDE -->'
|
||||
)
|
||||
|
||||
markdown = re.sub(TAG_REGEX_PATTERN, found_import_markdown_tag, markdown)
|
||||
markdown = re.sub(INCLUDE_TAG_REGEX, found_include_tag, markdown)
|
||||
markdown = re.sub(INCLUDEMARKDOWN_TAG_REGEX, found_includemarkdown_tag, markdown)
|
||||
return markdown
|
||||
|
||||
Reference in New Issue
Block a user