-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New manpage format #1921
base: gh-pages
Are you sure you want to change the base?
New manpage format #1921
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ gem "rss" | |
gem "asciidoctor", "~> 2.0.0" | ||
gem "nokogiri" | ||
gem "diffy" | ||
gem "parslet" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
require 'asciidoctor' | ||
require 'asciidoctor/extensions' | ||
require 'asciidoctor/converter/html5' | ||
require 'parslet' | ||
|
||
module Git | ||
module Documentation | ||
class AdocSynopsisQuote < Parslet::Parser | ||
# parse a string like "git add -p [--root=<path>]" as series of tokens keywords, grammar signs and placeholders | ||
# where placeholders are UTF-8 words separated by '-', enclosed in '<' and '>' | ||
rule(:space) { match('[\s\t\n ]').repeat(1) } | ||
rule(:space?) { space.maybe } | ||
rule(:keyword) { match('[-a-zA-Z0-9:+=~@,\./_\^\$\'"\*%!{}#]').repeat(1) } | ||
rule(:placeholder) { str('<') >> match('[[:word:]]|-').repeat(1) >> str('>') } | ||
rule(:opt_or_alt) { match('[\[\] |()]') >> space? } | ||
rule(:ellipsis) { str('...') >> match('\]|$').present? } | ||
rule(:grammar) { opt_or_alt | ellipsis } | ||
rule(:ignore) { match('[\'`]') } | ||
|
||
rule(:token) do | ||
grammar.as(:grammar) | placeholder.as(:placeholder) | space.as(:grammar) | | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. grammar and space are left unchanged, so I put them in the same bag. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You mean It still might be clearer to introduce a corresponding |
||
ignore.as(:ignore) | keyword.as(:keyword) | ||
end | ||
rule(:tokens) { token.repeat(1) } | ||
root(:tokens) | ||
end | ||
|
||
class EscapedSynopsisQuote < Parslet::Parser | ||
rule(:space) { match('[\s\t\n ]').repeat(1) } | ||
rule(:space?) { space.maybe } | ||
rule(:keyword) { match('[-a-zA-Z0-9:+=~@,\./_\^\$\'"\*%!{}#]').repeat(1) } | ||
rule(:placeholder) { str('<') >> match('[[:word:]]|-').repeat(1) >> str('>') } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this is the only difference to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I haven't tried to use inherit between my classes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I hope it will work, otherwise I'd have caused you a lot of effort for nothing in return. |
||
rule(:opt_or_alt) { match('[\[\] |()]') >> space? } | ||
rule(:ellipsis) { str('...') >> match('\]|$').present? } | ||
rule(:grammar) { opt_or_alt | ellipsis } | ||
rule(:ignore) { match('[\'`]') } | ||
|
||
rule(:token) do | ||
grammar.as(:grammar) | placeholder.as(:placeholder) | space.as(:grammar) | | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, should this be |
||
ignore.as(:ignore) | keyword.as(:keyword) | ||
end | ||
rule(:tokens) { token.repeat(1) } | ||
root(:tokens) | ||
end | ||
|
||
class SynopsisQuoteToAdoc < Parslet::Transform | ||
rule(grammar: simple(:grammar)) { grammar.to_s } | ||
rule(keyword: simple(:keyword)) { "{empty}`#{keyword}`{empty}" } | ||
rule(placeholder: simple(:placeholder)) { "__#{placeholder}__" } | ||
rule(ignore: simple(:ignore)) { '' } | ||
end | ||
|
||
class SynopsisQuoteToHtml5 < Parslet::Transform | ||
rule(grammar: simple(:grammar)) { grammar.to_s } | ||
rule(keyword: simple(:keyword)) { "<code>#{keyword}</code>" } | ||
rule(placeholder: simple(:placeholder)) { "<em>#{placeholder}</em>" } | ||
rule(ignore: simple(:ignore)) { '' } | ||
end | ||
|
||
class SynopsisConverter | ||
def convert(parslet_parser, parslet_transform, reader, logger = nil) | ||
reader.lines.map do |l| | ||
parslet_transform.apply(parslet_parser.parse(l)).join | ||
end.join("\n") | ||
rescue Parslet::ParseFailed | ||
logger&.info "synopsis parsing failed for '#{reader.lines.join(' ')}'" | ||
reader.lines.map do |l| | ||
parslet_transform.apply(placeholder: l) | ||
end.join("\n") | ||
end | ||
end | ||
|
||
class SynopsisBlock < Asciidoctor::Extensions::BlockProcessor | ||
use_dsl | ||
named :synopsis | ||
parse_content_as :simple | ||
|
||
def process(parent, reader, attrs) | ||
outlines = SynopsisConverter.new.convert( | ||
AdocSynopsisQuote.new, | ||
SynopsisQuoteToAdoc.new, | ||
reader, | ||
parent.document.logger | ||
) | ||
create_block parent, :verse, outlines, attrs | ||
end | ||
end | ||
|
||
# register a html5 converter that takes in charge | ||
# to convert monospaced text into Git style synopsis | ||
class GitHTMLConverter < Asciidoctor::Converter::Html5Converter | ||
extend Asciidoctor::Converter::Config | ||
register_for 'html5' | ||
|
||
def convert_inline_quoted(node) | ||
if node.type == :monospaced | ||
SynopsisConverter.new.convert( | ||
EscapedSynopsisQuote.new, | ||
SynopsisQuoteToHtml5.new, | ||
node.text, | ||
node.document.logger | ||
) | ||
else | ||
open, close, tag = QUOTE_TAGS[node.type] | ||
if node.id | ||
class_attr = node.role ? %( class="#{node.role}") : '' | ||
if tag | ||
%(#{open.chop} id="#{node.id}"#{class_attr}>#{node.text}#{close}) | ||
else | ||
%(<span id="#{node.id}"#{class_attr}>#{open}#{node.text}#{close}</span>) | ||
end | ||
elsif node.role | ||
if tag | ||
%(#{open.chop} class="#{node.role}">#{node.text}#{close}) | ||
else | ||
%(<span class="#{node.role}">#{open}#{node.text}#{close}</span>) | ||
end | ||
else | ||
%(#{open}#{node.text}#{close}) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
Asciidoctor::Extensions.register do | ||
block Git::Documentation::SynopsisBlock | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have to admit that I was quite thrown by the syntax, especially the
>>
one. https://kschiess.github.io/parslet/parser.html to the rescue (which we might want to link to, in a code comment).Narrators voice: The
>>
indicates a "simple sequence", for examplestr('...') >> match('\]|$').present?
means "first match three periods, then ensure that they are either followed by a closing bracket or they are at the end.