Class: GitIncludeMacro

Inherits:
Asciidoctor::Extensions::IncludeProcessor
  • Object
show all
Defined in:
lib/asciidoctor-git-include.rb

Instance Method Summary collapse

Instance Method Details

#handles?(target) ⇒ Boolean

Returns:

  • (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
# 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")

  cmd = %(git -C #{repository} show #{revision}:#{target})
  if (as_diff)
    cmd = %(git -C #{repository} diff #{diff_revision}:#{target} #{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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/asciidoctor-git-include.rb', line 33

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