Skip to content

Commit

Permalink
Add ability to include snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
PerilousApricot committed Feb 13, 2024
1 parent 96f2dce commit 2e65245
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions docs/_includes/includesnippet
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{%- comment -%}{% raw %}<!--
Includes the specified lines from an include file.

Usage:
{% include includelines filename=PATH start=INT count=INT %}

filename: path to file under _includes
start: first line to include, starting at 1
starttext: begin with line containing specified text
count: number of lines to include
endtext: ends with line containing specified text

Example:

{% include includelines filename='src/HelloWorld.java' start=10 count=5 %}

-->{% endraw %}{%- endcomment -%}
{%- capture filecontent -%}
{%- include {{include.filename}} -%}
{%- endcapture -%}
{%- assign lines = filecontent | rstrip | newline_to_br | split: '<br />' -%}
{%- 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 -%}
34 changes: 31 additions & 3 deletions docs/includetest.md
Original file line number Diff line number Diff line change
Expand Up @@ -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' %}
```

0 comments on commit 2e65245

Please sign in to comment.