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 26 27 28 29 30 31 32 |
# 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', '') as_diff = attributes.value?('diff') || attributes.key?('diff') diff_revision = attributes.fetch('diff', "#{revision}~1") target_before = attributes.fetch('difftarget', target) ignore_whitespaces_option = attributes.value?('ignorewhitespaces') || attributes.key?('ignorewhitespaces') ? '--ignore-cr-at-eol --ignore-space-at-eol -w -b --ignore-blank-lines' : '' cmd = %(git -C #{repository} show #{revision}:#{target}) if (as_diff) cmd = %(git -C #{repository} diff #{ignore_whitespaces_option} #{diff_revision}:#{target_before} #{revision}:#{target}) end 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
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/asciidoctor-git-include.rb', line 35 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 |