Skip to content
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

Open
wants to merge 2 commits into
base: gh-pages
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ gem "rss"
gem "asciidoctor", "~> 2.0.0"
gem "nokogiri"
gem "diffy"
gem "parslet"
2 changes: 1 addition & 1 deletion assets/sass/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ $baseurl: "{{ .Site.BaseURL }}{{ if (and (ne .Site.BaseURL "/") (ne .Site.BaseUR

code {
display: inline;
padding: 0 5px;
padding: 0 0;
}

pre {
Expand Down
3 changes: 0 additions & 3 deletions assets/sass/typography.scss
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,6 @@ blockquote {
}

code {
@include border-radius(3px);
display: block;
padding: 10px 15px 13px;
margin-bottom: 1em;
Expand All @@ -260,8 +259,6 @@ code {
line-height: $fixed-width-line-height;
font-variant-ligatures: none;
color: $orange;
background-color: #fff;
border: solid 1px #efeee6;
}

// Quotes
Expand Down
129 changes: 129 additions & 0 deletions script/asciidoctor-extensions.rb
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('[\'`]') }
Comment on lines +11 to +18
Copy link
Member

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 example str('...') >> match('\]|$').present? means "first match three periods, then ensure that they are either followed by a closing bracket or they are at the end.


rule(:token) do
grammar.as(:grammar) | placeholder.as(:placeholder) | space.as(:grammar) |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be space.as(:space)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean grammar is handled by this line and this line by leaving the original text unchanged?

It still might be clearer to introduce a corresponding rule(space: simple(:space)) { space.to_s } line (and to let SynopsisQuoteToHtml5 inherit from SynopsisQuoteToAdoc, overriding only keyword and placeholder).

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('&lt;') >> match('[[:word:]]|-').repeat(1) >> str('&gt;') }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is the only difference to AdocSynopsisQuote, why not use class EscapedSynopsisQuote <AdocSynopsisQuote and override just this rule?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I haven't tried to use inherit between my classes.

Copy link
Member

Choose a reason for hiding this comment

The 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) |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, should this be space.as(:space)?

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
1 change: 1 addition & 0 deletions script/update-docs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require 'yaml'
require 'diffy'
require_relative "version"
require_relative 'asciidoctor-extensions'

SITE_ROOT = File.join(File.expand_path(File.dirname(__FILE__)), '../')
DOCS_INDEX_FILE = "#{SITE_ROOT}external/docs/content/docs/_index.html"
Expand Down
Loading