From 2e65245e0004c5d5b0f3055502e3d47aa24c6e6f Mon Sep 17 00:00:00 2001 From: Andrew Melo Date: Tue, 13 Feb 2024 15:45:29 -0600 Subject: [PATCH] Add ability to include snippets --- docs/_config.yml | 1 + docs/_includes/includesnippet | 35 +++++++++++++++++++++++++++++++++++ docs/includetest.md | 34 +++++++++++++++++++++++++++++++--- 3 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 docs/_includes/includesnippet diff --git a/docs/_config.yml b/docs/_config.yml index 4cbf70f..4f703bb 100644 --- a/docs/_config.yml +++ b/docs/_config.yml @@ -33,6 +33,7 @@ color_scheme: dark #plugins: # - jekyll-feed +includes_dir: . # Exclude from processing. # The following items will not be processed, by default. # Any item listed under the `exclude:` key here will be automatically added to diff --git a/docs/_includes/includesnippet b/docs/_includes/includesnippet new file mode 100644 index 0000000..ecd1ff1 --- /dev/null +++ b/docs/_includes/includesnippet @@ -0,0 +1,35 @@ +{%- comment -%}{% raw %}{% endraw %}{%- endcomment -%} +{%- capture filecontent -%} +{%- include {{include.filename}} -%} +{%- endcapture -%} +{%- assign lines = filecontent | rstrip | newline_to_br | split: '
' -%} +{%- assign start = {{include.start}} -%} +{%- assign count = {{include.count}} -%} +{%- for line in lines -%} +{%- if line contains {{include.starttext}} -%} +{%- assign start = forloop.index | minus: 1 -%} +{%- endif -%} +{%- if line contains {{include.endtext}} -%} +{%- assign end = forloop.index | decrement -%} +{%- assign count = end | minus: start -%} +{%- endif -%} +{%- endfor -%} +{%- for line in lines offset: {{start}} limit: {{count}} -%} + {{- line | rstrip -}} +{%- endfor -%} diff --git a/docs/includetest.md b/docs/includetest.md index a8faaff..6833829 100644 --- a/docs/includetest.md +++ b/docs/includetest.md @@ -3,10 +3,38 @@ hidden: true --- +All four lines ```python -{% include includelines filename='testfile.py' method='main()' start=1 count=3 %} +{% include _includes/includesnippet filename='_includes/testfile.py' %} ``` -{% include includemethod filename='testfile.py' method='main()' before=0 after=3 %} - +two lines +```python +{% include _includes/includesnippet filename='_includes/testfile.py' count=2 %} +``` + +start at line 2 +```python +{% include _includes/includesnippet filename='_includes/testfile.py' start=2 %} +``` + +start at line 1, but do two lines +```python +{% include _includes/includesnippet filename='_includes/testfile.py' start=1 count=2 %} +``` + +get main +```python +{% include _includes/includesnippet filename='_includes/testfile.py' starttext='main()' endtext='return' %} +``` + +get comment to return +```python +{% include _includes/includesnippet filename='_includes/testfile.py' starttext='A comment' endtext='return' %} +``` + +get comment to assignment +```python +{% include _includes/includesnippet filename='_includes/testfile.py' starttext='A comment' endtext='p = 2' %} +```