File tree 6 files changed +60
-7
lines changed
spec/lib/jekyll_github_sample
6 files changed +60
-7
lines changed Original file line number Diff line number Diff line change 1
1
PATH
2
2
remote: .
3
3
specs:
4
- jekyll_github_sample (0.2 .0 )
4
+ jekyll_github_sample (0.3 .0 )
5
5
activesupport (~> 4.0 )
6
6
jekyll (~> 3.0 )
7
7
@@ -76,4 +76,4 @@ DEPENDENCIES
76
76
rspec
77
77
78
78
BUNDLED WITH
79
- 1.13.6
79
+ 1.13.7
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ Two Jekyll Liquid tags to display a code sample from a file in a public Github r
5
5
6
6
# Install
7
7
8
- - First add the gem to your ` Gemfile `
8
+ - First add the gem to your ` Gemfile `
9
9
```
10
10
gem 'jekyll_github_sample'
11
11
```
@@ -30,6 +30,16 @@ A [write up](https://bwillis.github.io/2014/05/28/include-github-repo-code-in-je
30
30
* START_LINE_NUMBER - (optional) number that is the first line to include (0 based)
31
31
* END_LINE_NUMBER - (optional) number that is the last line to include, if excluded will read to end of file
32
32
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
+
33
43
# github_sample_ref Usage
34
44
```
35
45
{% github_sample_ref URL_WITH_USERNAME_REPO_AND_FILE %}
Original file line number Diff line number Diff line change @@ -11,15 +11,19 @@ class CodeTag < ::Liquid::Tag
11
11
def initialize ( tag_name , params , tokens )
12
12
github_file_path , @line_start , @line_end = params . split
13
13
@github_file = FileHelper . new ( github_file_path )
14
- @line_start , @line_end = determine_line_numbers ( @line_start , @line_end )
15
14
super
16
15
end
17
16
18
17
def render ( context )
19
18
all_lines = cache . fetch ( @github_file . raw_uri ) do
20
19
open ( @github_file . raw_uri ) . readlines
21
20
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
23
27
lines = remove_common_indentation ( lines )
24
28
lines . join
25
29
end
Original file line number Diff line number Diff line change @@ -18,5 +18,24 @@ def remove_common_indentation(lines)
18
18
line . length == 1 ? line : line [ leading_spaces . min ..-1 ]
19
19
end
20
20
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
21
40
end
22
41
end
Original file line number Diff line number Diff line change 1
1
module JekyllGithubSample
2
- VERSION = '0.2 .0'
2
+ VERSION = '0.3 .0'
3
3
end
Original file line number Diff line number Diff line change 33
33
end
34
34
end
35
35
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
You can’t perform that action at this time.
0 commit comments