Skip to content

Commit b0d0e88

Browse files
committed
Support extracting content based on tags as well as line numbers.
1 parent 2951b97 commit b0d0e88

File tree

6 files changed

+60
-7
lines changed

6 files changed

+60
-7
lines changed

Gemfile.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
jekyll_github_sample (0.2.0)
4+
jekyll_github_sample (0.3.0)
55
activesupport (~> 4.0)
66
jekyll (~> 3.0)
77

@@ -76,4 +76,4 @@ DEPENDENCIES
7676
rspec
7777

7878
BUNDLED WITH
79-
1.13.6
79+
1.13.7

README.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Two Jekyll Liquid tags to display a code sample from a file in a public Github r
55

66
# Install
77

8-
- First add the gem to your `Gemfile`
8+
- First add the gem to your `Gemfile`
99
```
1010
gem 'jekyll_github_sample'
1111
```
@@ -30,6 +30,16 @@ A [write up](https://bwillis.github.io/2014/05/28/include-github-repo-code-in-je
3030
* START_LINE_NUMBER - (optional) number that is the first line to include (0 based)
3131
* END_LINE_NUMBER - (optional) number that is the last line to include, if excluded will read to end of file
3232

33+
One can also specify the lines to include based on markings in the file itself.
34+
This is done by invoking
35+
36+
```
37+
{% github_sample URL_WITH_USERNAME_REPO_AND_FILE tag:TAG_NAME %}
38+
```
39+
40+
and placing the strings `[START TAG_NAME]` and `[END TAG_NAME]` anywhere in the lines immediately before and after the content you wish to include.
41+
42+
3343
# github_sample_ref Usage
3444
```
3545
{% github_sample_ref URL_WITH_USERNAME_REPO_AND_FILE %}

lib/jekyll_github_sample/code_tag.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,19 @@ class CodeTag < ::Liquid::Tag
1111
def initialize(tag_name, params, tokens)
1212
github_file_path, @line_start, @line_end = params.split
1313
@github_file = FileHelper.new(github_file_path)
14-
@line_start, @line_end = determine_line_numbers(@line_start, @line_end)
1514
super
1615
end
1716

1817
def render(context)
1918
all_lines = cache.fetch(@github_file.raw_uri) do
2019
open(@github_file.raw_uri).readlines
2120
end
22-
lines = all_lines[@line_start..@line_end]
21+
if @line_start.respond_to?(:match) and tag_match = @line_start.match(/^tag:(.*)/)
22+
lines = extract_tagged_lines(all_lines, tag_match[1])
23+
else
24+
@line_start, @line_ends = determine_line_numbers(@line_start, @line_end)
25+
lines = all_lines[@line_start..@line_end]
26+
end
2327
lines = remove_common_indentation(lines)
2428
lines.join
2529
end

lib/jekyll_github_sample/text_utils.rb

+19
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,24 @@ def remove_common_indentation(lines)
1818
line.length == 1 ? line : line[leading_spaces.min..-1]
1919
end
2020
end
21+
22+
def extract_tagged_lines(lines, tag)
23+
start_tag = "[START #{tag}]"
24+
end_tag = "[END #{tag}]"
25+
tagged_lines = []
26+
in_tagged_content = false
27+
lines.each do |line|
28+
if in_tagged_content
29+
if line.include? end_tag
30+
in_tagged_content = false
31+
else
32+
tagged_lines << line
33+
end
34+
else
35+
in_tagged_content = line.include? start_tag
36+
end
37+
end
38+
tagged_lines
39+
end
2140
end
2241
end

lib/jekyll_github_sample/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module JekyllGithubSample
2-
VERSION = '0.2.0'
2+
VERSION = '0.3.0'
33
end

spec/lib/jekyll_github_sample/text_utils_spec.rb

+21-1
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,24 @@
3333
end
3434
end
3535

36-
end
36+
context '#extract_tagged_lines' do
37+
let(:lines) { [
38+
'header',
39+
'[START tag]',
40+
'content 1',
41+
'content 2',
42+
'[END tag]',
43+
'footer 1',
44+
'footer 2'
45+
] }
46+
subject { text_utils.extract_tagged_lines(lines, 'tag') }
47+
48+
it 'extracts content' do
49+
should =~ [
50+
'content 1',
51+
'content 2'
52+
]
53+
end
54+
end
55+
56+
end

0 commit comments

Comments
 (0)