Skip to content

Asciidoctor: Add "Edit Me" Links #526

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

Merged
merged 1 commit into from
Jan 7, 2019
Merged
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
6 changes: 2 additions & 4 deletions lib/ES/Util.pm
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,8 @@ sub build_chunked {
'-d' => 'book',
'-a' => 'showcomments=1',
'-a' => "lang=$lang",
'-a' => 'base_edit_url=' . $edit_url,
'-a' => 'root_dir=' . $root_dir,
$private ? ( '-a' => 'edit_url!' ) : (),
$private ? () : ( '-a' => "edit_url=$edit_url" ),
Copy link
Member Author

Choose a reason for hiding this comment

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

I've flipped this around because just the presence of the edit_url attribute is enough for asciidoctor.

# Disable warning on missing attributes because we have
# missing attributes!
# '-a' => 'attribute-missing=warn',
Expand Down Expand Up @@ -206,10 +205,9 @@ sub build_single {
'-d' => $type,
'-a' => 'showcomments=1',
'-a' => "lang=$lang",
'-a' => 'base_edit_url=' . $edit_url,
'-a' => 'root_dir=' . $root_dir,
$private ? () : ( '-a' => "edit_url=$edit_url" ),
'-a' => 'asciidoc-dir=' . $asciidoc_dir,
$private ? ( '-a' => 'edit_url!' ) : (),
# Disable warning on missing attributes because we have
# missing attributes!
# '-a' => 'attribute-missing=warn',
Expand Down
37 changes: 37 additions & 0 deletions resources/asciidoctor/lib/edit_me/extension.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require_relative '../scaffold.rb'

include Asciidoctor

##
# TreeProcessor extension to automatically add "Edit Me" links to appropriate
# spots in the documentation.
class EditMe < TreeProcessorScaffold
include Logging

def process document
logger.error("sourcemap is required") unless document.sourcemap
if document.attributes['edit_url']
super
end
end

def process_block block
if [:preamble, :section, :floating_title].include? block.context
def block.title
url = @document.attributes['edit_url']
url += '/' unless url.end_with?('/')
url += source_path
"#{super}<ulink role=\"edit_me\" url=\"#{url}\">Edit me</ulink>"
end
if :preamble == block.context
def block.source_path
document.source_location.path
end
else
def block.source_path
source_location.path
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'asciidoctor/extensions'
require_relative '../scaffold.rb'

include Asciidoctor

Expand All @@ -23,23 +23,15 @@
# <1> The count of categories that were matched
# <2> The categories retrieved
#
class ElasticCompatTreeProcessor < Extensions::TreeProcessor
def process document
process_blocks document
nil
end

def process_blocks block
class ElasticCompatTreeProcessor < TreeProcessorScaffold
def process_block block
if block.context == :listing && block.style == "source" &&
false == block.subs.include?(:specialcharacters)
# callouts have to come *after* special characters
had_callouts = block.subs.delete(:callouts)
block.subs << :specialcharacters
block.subs << :callouts if had_callouts
end
for subblock in block.context == :dlist ? block.blocks.flatten : block.blocks
process_blocks subblock
end
end
end

5 changes: 5 additions & 0 deletions resources/asciidoctor/lib/extensions.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
require_relative 'added/extension'
require_relative 'edit_me/extension'
require_relative 'elastic_compat_tree_processor/extension'
require_relative 'elastic_compat_preprocessor/extension'
require_relative 'elastic_include_tagged/extension'

Extensions.register do
# Enable storing the source locations so we can look at them. This is required
# for EditMe to get a nice location.
document.sourcemap = true
preprocessor ElasticCompatPreprocessor
treeprocessor EditMe
treeprocessor ElasticCompatTreeProcessor
include_processor ElasticIncludeTagged
block_macro AddedBlock
Expand Down
23 changes: 23 additions & 0 deletions resources/asciidoctor/lib/scaffold.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'asciidoctor/extensions'

include Asciidoctor

##
# Scaffolding for TreeProcessor extensions to automatically iterate.
class TreeProcessorScaffold < Extensions::TreeProcessor
def process_block document
raise ::NotImplementedError, %(TreeProcessorScaffold subclass must implement ##{__method__} method)
end

def process document
process_blocks document
nil
end

def process_blocks block
process_block block
for subblock in block.context == :dlist ? block.blocks.flatten : block.blocks
process_blocks subblock
end
end
end
Loading