Skip to content

Commit

Permalink
Add test for including files
Browse files Browse the repository at this point in the history
  • Loading branch information
PerilousApricot committed Feb 13, 2024
1 parent bba8f28 commit 96f2dce
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ gem "minima", "~> 2.5"
# uncomment the line below. To upgrade, run `bundle update github-pages`.
gem "github-pages", group: :jekyll_plugins
gem "just-the-docs", "~> 0.7.0"
gem "liquid"
# If you have any plugins, put them here!
group :jekyll_plugins do
gem "jekyll-feed", "~> 0.12"
Expand Down
23 changes: 23 additions & 0 deletions docs/_includes/includelines
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{% 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
count: number of lines to include

Example:

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

-->{% endraw %}{% endcomment %}

{% capture filecontent %}
{% include {{include.filename}} %}
{% endcapture %}

{% assign lines = filecontent | newline_to_br | split: '<br />' %}

{% for line in lines offset:{{include.start}} limit:{{include.count}} %}{{ line }}{% endfor %}
60 changes: 60 additions & 0 deletions docs/_includes/includemethod
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{% comment %}{% raw %}<!--
Includes a method snippet from an include file of C / Java type language.

Usage:
{% include includemethod filename=PATH method=STRING [before=INT] [after=INT] %}

filename: path to file under _includes
method: method or other string to match
before: lines to include before the start of the method
after: lines to include after the method's final closing bracket

Includes lines starting at where the string 'method' is found, and continues
until a matching curly bracket is found. The before / after arguments can be
used to include further lines.

Example:

{% include includemethod filename='src/HelloWorld.java' method='test()' before=2 after=1 %}

-->{% endraw %}{% endcomment %}{% capture unused %}

{% capture filecontent %}
{% include {{include.filename}} %}
{% endcapture %}

{% assign start = 0 %}
{% assign braketlevel = 0 %}

{% assign lines = filecontent | newline_to_br | split: '<br />' %}
{% for line in lines %}
{% if line contains {{include.method}} %}
{% assign start = forloop.index | minus: 1 %}
{% endif %}

{% if start > 0 %}
{%if line contains '{' %}
{% assign braketlevel = braketlevel | plus: 1 %}
{% endif %}

{% if line contains '}' %}
{% assign braketlevel = braketlevel | minus: 1 %}
{% endif %}

{% if braketlevel == 0 %}
{% assign count = forloop.index | minus: start %}
{% break %}
{% endif %}
{% endif %}
{% endfor %}

{% if include.before %}
{% assign start = start | minus: include.before %}
{% assign count = count | plus: include.before %}
{% endif %}

{% if include.after %}
{% assign count = count | plus: include.after %}
{% endif %}

{% endcapture %}{% assign unused = nil %}{% for line in lines offset:start limit:count %}{{ line }}{% endfor %}
12 changes: 12 additions & 0 deletions docs/includetest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
hidden: true
---


```python
{% include includelines filename='testfile.py' method='main()' start=1 count=3 %}
```

{% include includemethod filename='testfile.py' method='main()' before=0 after=3 %}


0 comments on commit 96f2dce

Please sign in to comment.