Class: GitIncludeMacro
- Inherits:
-
Asciidoctor::Extensions::IncludeProcessor
- Object
- Asciidoctor::Extensions::IncludeProcessor
- GitIncludeMacro
- Defined in:
- lib/asciidoctor-git-include.rb
Instance Method Summary collapse
- #handles?(target) ⇒ Boolean
- #process(doc, reader, target, attributes) ⇒ Object
- #process_line_selection(content, lines) ⇒ Object
Instance Method Details
#handles?(target) ⇒ Boolean
8 9 10 |
# File 'lib/asciidoctor-git-include.rb', line 8 def handles? target (target.start_with? 'git@') end |
#process(doc, reader, target, attributes) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/asciidoctor-git-include.rb', line 12 def process doc, reader, target, attributes target.slice! "git@" repository = attributes.fetch('repository', '.') revision = attributes.fetch('revision', 'HEAD') lines = attributes.fetch('lines', '') cmd = %(git -C #{repository} show #{revision}:#{target}) content = %x(#{cmd}) content = process_line_selection(content, lines) unless lines == "" reader.push_include content, target, target, 1, attributes reader end |
#process_line_selection(content, lines) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/asciidoctor-git-include.rb', line 28 def process_line_selection content, lines snipped_content = [] selected_lines = [] text = content.split(/\n/) lines.split(/[,;]/).each do |linedef| if linedef.include?('..') from, to = linedef.split('..', 2).map {|it| it.to_i } to = text.length if to == -1 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 |