Add lines attribute for Git blob include processor

This commit is contained in:
Gabriel Arazas 2023-05-27 16:15:49 +08:00
parent fb5dac150b
commit 07faed03a1
2 changed files with 18 additions and 1 deletions

View File

@ -32,6 +32,9 @@ This is only effective if `diff` option is given.
- `other` is the other commit to be compared with.
This is only effective if `diff` option is given.
- `lines` get the specified lines from the path.
It uses the same parser as the line specifier from link:https://docs.asciidoctor.org/asciidoc/latest/verbatim/highlight-lines/[highlighting select lines] so the syntax is the same.
There's also a couple of link:https://docs.asciidoctor.org/asciidoc/latest/attributes/options/[options] for this component.
You can give the following options through `opts` attribute (i.e., `opts="diff,reverse"`).
@ -61,3 +64,5 @@ The following examples assume that `doccontentref` points to `content/posts/samp
- `include::git:{doccontentref}[opts="diff", path=Gemfile]` should include a diff of the Gemfile from `content/posts/sample` branch (assuming that Gemfile has changed).
- `include::git:non-existing-rev[opts="diff"]` should result in a warning with the non-existing revision.
- `include::git:{doccontentref}[path=README, lines=2..5]` should transclude the README file from lines 2-5.

View File

@ -41,7 +41,19 @@ class GitBlobIncludeProcessor < Asciidoctor::Extensions::IncludeProcessor
git_object.target.tree.path attrs['path']
end
reader.push_include repo.lookup(inner_entry[:oid]).content
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}"