Class: GithubPrivateUriIncludeProcessor
- Inherits:
-
Extensions::IncludeProcessor
- Object
- Extensions::IncludeProcessor
- GithubPrivateUriIncludeProcessor
- Defined in:
- lib/asciidoctor-github-include.rb
Instance Method Summary collapse
- #handles?(target) ⇒ Boolean
- #process(doc, reader, target, attributes) ⇒ Object
- #process_line_selection(text, lines, target) ⇒ Object
-
#process_tags(text, tags, target) ⇒ Object
We need to process tags.
Instance Method Details
#handles?(target) ⇒ Boolean
7 8 9 |
# File 'lib/asciidoctor-github-include.rb', line 7 def handles? target (target.start_with? 'https://raw.githubusercontent.com') end |
#process(doc, reader, target, attributes) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/asciidoctor-github-include.rb', line 11 def process doc, reader, target, attributes = [attributes["tag"]] if attributes.key? "tag" unless attributes["tag"] == "" = attributes["tags"].split(DataDelimiterRx) if attributes.key? "tags" unless attributes["tags"] == "" lines = attributes["lines"] unless attributes["lines"] == "" if lines && warn %(asciidoctor: WARNING: Tag selection #{} in #{target} was ignored because line selection was specified.) end # Fetch the file to be included begin doc.attr('github-access-token').nil? ? content = (open target).readlines : content = (open target, "Authorization" => "token " + doc.attr('github-access-token')).readlines rescue warn %(asciidoctor: WARNING: Failed to retrieve GitHub URI #{target}. Did you set :github-access-token:?) content = "WARNING: Failed to retrieve GitHub URI link:#{target}[]" return reader.push_include content, target, target, 1, attributes end # process the lines and tags attributes content = process_line_selection(content, lines, target) if lines content = (content, , target) if unless lines # push the lines onto the reader and return it reader.push_include content, target, target, 1, attributes reader end |
#process_line_selection(text, lines, target) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/asciidoctor-github-include.rb', line 79 def process_line_selection text, lines, target snipped_content = [] selected_lines = [] lines.split(DataDelimiterRx).each do |linedef| if linedef.include?('..') from, to = linedef.split('..', 2).map {|it| it.to_i } to = text.length if to == -1 # -1 as a closing length means end of file selected_lines.concat ::Range.new(from, to).to_a else selected_lines << linedef.to_i end end selected_lines.sort.uniq.each do |i| snipped_content << text[i-1] end snipped_content end |
#process_tags(text, tags, target) ⇒ Object
We need to process tags. This is a very naïve implementation to start with, and only supports the case where there is exactly one opening and closing instance of the tag in the file.
text - the text to be processed, as an array of lines tags - an array of tags to get target - the URI of the object being fetched (only used to check the extension)
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/asciidoctor-github-include.rb', line 47 def text, , target snipped_content = [] # Asciidoctor provides a map of (file extension) => (opening and closing tags for # the file's circumfix comments). Use it to check for this case and get the right # suffix if needed. target_extension = target.slice (target.rindex '.'), target.length if (circ_cmt = CIRCUMFIX_COMMENTS[target_extension]) circumfix_suffix = circ_cmt[:suffix] end .each do |tag| if circumfix_suffix tag_open = text.index{|line| line.chomp.end_with? %(tag::#{tag}[] #{circumfix_suffix})} tag_close = text.index{|line| line.chomp.end_with? %(end::#{tag}[] #{circumfix_suffix})} else tag_open = text.index{|line| line.chomp.end_with? %(tag::#{tag}[])} tag_close = text.index{|line| line.chomp.end_with? %(end::#{tag}[])} end if (!tag_open && !tag_close) warn %(asciidoctor: WARNING: Tag #{tag} not found in included GitHub URI #{target}.) elsif (!tag_open && tag_close) warn %(asciidoctor: WARNING: Tag #{tag} not found in included GitHub URI #{target}, but end::[] tag was found.) elsif (tag_open && !tag_close) warn %(asciidoctor: WARNING: Closing tag for tag #{tag} not found in included GitHub URI #{target}.) end snipped_content += text[tag_open+1..tag_close-1] unless (!tag_open || !tag_close) end snipped_content end |