Class: Asciidoctor::Foodogsquared::Extensions::GitBlobIncludeProcessor

Inherits:
Extensions::IncludeProcessor
  • Object
show all
Defined in:
lib/asciidoctor/foodogsquared/extensions/git-blob-include-processor.rb

Instance Method Summary collapse

Instance Method Details

#handles?(target) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/asciidoctor/foodogsquared/extensions/git-blob-include-processor.rb', line 7

def handles?(target)
  target.start_with? 'git:'
end

#process(doc, reader, target, attrs) ⇒ 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/asciidoctor/foodogsquared/extensions/git-blob-include-processor.rb', line 11

def process(doc, reader, target, attrs)
  attrs['gitrepo'] ||= doc.attributes['gitrepo'] || doc.base_dir
  repo = Rugged::Repository.discover(attrs['gitrepo'])

  git_object_ref = target.delete_prefix 'git:'
  git_object_ref = doc.attributes['doccontentref'] if git_object_ref.empty?

  begin
    git_object = repo.rev_parse git_object_ref

    if attrs.key? 'diff-option'
      options = {}

      options[:paths] = attrs['path'].split(';') if attrs.key? 'path'
      options[:context_lines] = attrs['context-lines'] if attrs.key? 'context-lines'
      options[:reverse] = true if attrs.key? 'reverse-option'

      if attrs.key? 'other'
        other = repo.rev_parse attrs['other'] || nil
        reader.push_include git_object.diff(other, **options).patch
      else
        reader.push_include git_object.diff(**options).patch
      end
    else
      inner_entry = case git_object.type
                    when :blob
                      git_object
                    when :commit
                      git_object.tree.path attrs['path']
                    when :tree
                      git_object.path attrs['path']
                    when :tag
                      git_object.target.tree.path attrs['path']
                    end

      content = repo.lookup(inner_entry[:oid]).content

      if attrs.key? 'lines'
        content_lines = content.lines
        new_content = +''
        doc.resolve_lines_to_highlight(content, attrs['lines']).each do |line_no|
          new_content << content_lines.at(line_no - 1)
        end

        content = new_content
      end

      reader.push_include content
    end
  rescue StandardError => e
    reader.push_include "Unresolved directive for '#{target}' with the following error:\n#{e}"
    warn e
  end

  reader
end