diff --git a/lib/asciidoctor/git-blob-include-processor/README.adoc b/lib/asciidoctor/git-blob-include-processor/README.adoc index ca5718b..6dfbe75 100644 --- a/lib/asciidoctor/git-blob-include-processor/README.adoc +++ b/lib/asciidoctor/git-blob-include-processor/README.adoc @@ -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. diff --git a/lib/asciidoctor/git-blob-include-processor/extension.rb b/lib/asciidoctor/git-blob-include-processor/extension.rb index fee79ab..0ea2040 100644 --- a/lib/asciidoctor/git-blob-include-processor/extension.rb +++ b/lib/asciidoctor/git-blob-include-processor/extension.rb @@ -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}"