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

#89 Add Wavedrom support #90

Merged
merged 1 commit into from
Jan 13, 2016
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
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ install:
- sudo pip install blockdiag actdiag seqdiag nwdiag
- sudo pip install shaape
- bundle install --path vendor/bundle --jobs=3 --retry=3
before_script:
- wget https://github.com/wavedrom/wavedrom.github.io/releases/download/v1.2.0/wavedrom-editor-v1.2.0-linux-x64.tar.gz -O wavedrom.tar.gz
- tar zxf wavedrom.tar.gz
- export PATH=$PATH:$PWD/WaveDromEditor/linux64
6 changes: 5 additions & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ These extensions allow you to embed plain text diagrams inside your AsciiDoc doc
- https://github.com/christiangoltz/shaape[Shaape]
- http://blockdiag.com[BlockDiag, SeqDiag, ActDiag, NwDiag]
- http://www.graphviz.org/content/dot-language[GraphViz DOT]
- http://wavedrom.com[WaveDrom]

The extension takes care of running the diagram processor to generate the images from the input text and insert them into the rendered document.

Expand Down Expand Up @@ -43,6 +44,7 @@ Certain diagram types require other tools to be installed seperately.
- PlantUML and Graphviz: the Graphviz `dot` tool.
- Shaape: the Shaape Python package
- Block/Seq/Act/Nw diag: the block/seq/act/nw diag Python packages
- WaveDrom: the https://github.com/wavedrom/wavedrom.github.io/releases[WaveDrom editor] application

== Usage

Expand All @@ -58,6 +60,7 @@ In your script you can then either require one or more of the following files:
. `asciidoctor-diagram/graphviz`: to enable the graphviz extension
. `asciidoctor-diagram/plantuml`: to enable the plantuml extension
. `asciidoctor-diagram/shaape`: to enable the shaape extension
. `asciidoctor-diagram/wavedrom`: to enable the wavedrom extension

Requiring one or more of these files will automatically register the extensions for all processed documents.
If you need more fine grained control over when the extensions are enabled or not, `asciidoctor-diagram/ditaa/extension`, `asciidoctor-diagram/graphviz/extension` and `asciidoctor-diagram/plantuml/extension` can be used instead.
Expand All @@ -73,6 +76,7 @@ Once the extensions are enabled the following block types becomes available for
- `graphviz`
- `plantuml`
- `shaape`
- `wavedrom`

Detailed descriptions of the supported syntax inside these blocks is available on the http://plantuml.sourceforge.net/[PlantUML], http://www.graphviz.org/content/dot-language[Graphviz] and http://ditaa.sourceforge.net/[ditaa] websites.

Expand All @@ -98,7 +102,7 @@ The diagram blocks support the following attributes:
. `format` (or 3rd position): the output format. PlantUML blocks support `png`, `svg` and `txt`. Graphviz, Shaape and BlockDiag support `png` and `svg`. Ditaa only supports `png`.

Once you have all of this in place and your original AsciiDoc file contains a diagram block, it's time to build it into an HTML file with Asciidoctor Diagram magic!
When executing Asciidoctor, you must reference the Adciidoctor Diagram library, otherwise your diagam blocks won't be recognized as such. When executing Asciidoctor from the command line, do it using the -r parameter to reference this external library:
When executing Asciidoctor, you must reference the Adciidoctor Diagram library, otherwise your diagram blocks won't be recognized as such. When executing Asciidoctor from the command line, do it using the -r parameter to reference this external library:

$ asciidoctor -r asciidoctor-diagram doc.adoc

Expand Down
3 changes: 2 additions & 1 deletion lib/asciidoctor-diagram.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
require 'asciidoctor-diagram/graphviz'
require 'asciidoctor-diagram/plantuml'
require 'asciidoctor-diagram/salt'
require 'asciidoctor-diagram/shaape'
require 'asciidoctor-diagram/shaape'
require 'asciidoctor-diagram/wavedrom'
2 changes: 1 addition & 1 deletion lib/asciidoctor-diagram/blockdiag/extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def self.define_processors(name, &init)

[:png, :svg].each do |f|
register_format(f, :image) do |c, p|
CliGenerator.generate(which(p, tool.downcase), c.to_s) do |tool_path, output_path|
CliGenerator.generate_stdin(which(p, tool.downcase), c.to_s) do |tool_path, output_path|
[tool_path, '-o', output_path, "-T#{f.to_s}", '-']
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/asciidoctor-diagram/graphviz/extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Graphviz
def self.included(mod)
[:png, :svg].each do |f|
mod.register_format(f, :image) do |c, p|
CliGenerator.generate(which(p, 'dot', 'graphvizdot'), c.to_s) do |tool_path, output_path|
CliGenerator.generate_stdin(which(p, 'dot', :attr_names => ['dot', 'graphvizdot']), c.to_s) do |tool_path, output_path|
[tool_path, "-o#{output_path}", "-T#{f.to_s}"]
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/asciidoctor-diagram/shaape/extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Shaape
def self.included(mod)
[:png, :svg].each do |f|
mod.register_format(f, :image) do |c, p|
CliGenerator.generate(which(p, 'shaape'), c.to_s) do |tool_path, output_path|
CliGenerator.generate_stdin(which(p, 'shaape'), c.to_s) do |tool_path, output_path|
[tool_path, '-o', output_path, '-t', f.to_s, '-']
end
end
Expand Down
29 changes: 28 additions & 1 deletion lib/asciidoctor-diagram/util/cli_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Asciidoctor
module Diagram
# @private
module CliGenerator
def self.generate(tool, code)
def self.generate_stdin(tool, code)
tool_name = File.basename(tool)

target_file = Tempfile.new(tool_name)
Expand All @@ -25,6 +25,33 @@ def self.generate(tool, code)
target_file.unlink
end
end

def self.generate_file(tool, code)
tool_name = File.basename(tool)

source_file = Tempfile.new(tool_name)
begin
File.write(source_file.path, code)

target_file = Tempfile.new(tool_name)
begin
target_file.close

args = yield tool, source_file.path, target_file.path

system(*args)
result_code = $?

raise "#{tool_name} image generation failed" unless result_code == 0

File.binread(target_file.path)
ensure
target_file.unlink
end
ensure
source_file.unlink
end
end
end
end
end
14 changes: 8 additions & 6 deletions lib/asciidoctor-diagram/util/which.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ module Asciidoctor
module Diagram
module Which
# @private
def self.which(cmd)
def self.which(cmd, options = {})
exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|

paths = (options[:path] || []) + ENV['PATH'].split(File::PATH_SEPARATOR)
paths.each do |path|
exts.each { |ext|
exe = File.join(path, "#{cmd}#{ext}")
return exe if File.executable? exe
Expand All @@ -13,15 +15,15 @@ def self.which(cmd)
nil
end

def which(parent_block, cmd, *attr_names)
attr_names.insert(0, cmd)
def which(parent_block, cmd, options = {})
attr_names = options[:attr_names] || [cmd]

cmd_var = '@' + cmd
cmd_var = '@' + attr_names[0]

cmd_path = instance_variable_get(cmd_var)
unless cmd_path
cmd_path = attr_names.map { |attr_name| parent_block.document.attributes[attr_name] }.find { |attr| !attr.nil? }
cmd_path = ::Asciidoctor::Diagram::Which.which(cmd) unless cmd_path && File.executable?(cmd_path)
cmd_path = ::Asciidoctor::Diagram::Which.which(cmd, :path => options[:path]) unless cmd_path && File.executable?(cmd_path)
raise "Could not find the '#{cmd}' executable in PATH; add it to the PATH or specify its location using the '#{attr_names[0]}' document attribute" unless cmd_path
instance_variable_set(cmd_var, cmd_path)
end
Expand Down
9 changes: 9 additions & 0 deletions lib/asciidoctor-diagram/wavedrom.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'asciidoctor/extensions'
require_relative 'version'

Asciidoctor::Extensions.register do
require_relative 'wavedrom/extension'

block Asciidoctor::Diagram::WavedromBlockProcessor, :wavedrom
block_macro Asciidoctor::Diagram::WavedromBlockMacroProcessor, :wavedrom
end
39 changes: 39 additions & 0 deletions lib/asciidoctor-diagram/wavedrom/extension.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require_relative '../extensions'
require_relative '../util/cli_generator'
require_relative '../util/which'

module Asciidoctor
module Diagram
# @private
module Wavedrom
include Which

def self.included(mod)
[:png, :svg].each do |f|
mod.register_format(f, :image) do |c, p|
if /darwin/ =~ RUBY_PLATFORM
wavedrom = which(p, 'WaveDromEditor.app', :attr_names => ['wavedrom'], :path => ['/Applications'])
if wavedrom
wavedrom = File.join(wavedrom, 'Contents/MacOS/nwjs')
end
else
wavedrom = which(p, 'WaveDromEditor', :attr_names => ['wavedrom'])
end

CliGenerator.generate_file(wavedrom, c.to_s) do |tool_path, input_path, output_path|
[tool_path, 'source', input_path, f.to_s, output_path]
end
end
end
end
end

class WavedromBlockProcessor < Extensions::DiagramBlockProcessor
include Wavedrom
end

class WavedromBlockMacroProcessor < Extensions::DiagramBlockMacroProcessor
include Wavedrom
end
end
end
4 changes: 2 additions & 2 deletions spec/shaape_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require_relative 'test_helper'

describe Asciidoctor::Diagram::ShaapeBlockMacroProcessor do
describe Asciidoctor::Diagram::ShaapeBlockMacroProcessor, :broken => /darwin/ =~ RUBY_PLATFORM do
it "should generate PNG images when format is set to 'png'" do
code = <<-eos
+--------+ +-------------+
Expand Down Expand Up @@ -40,7 +40,7 @@
end
end

describe Asciidoctor::Diagram::ShaapeBlockProcessor do
describe Asciidoctor::Diagram::ShaapeBlockProcessor, :broken => /darwin/ =~ RUBY_PLATFORM do
it "should generate PNG images when format is set to 'png'" do
doc = <<-eos
= Hello, Shaape!
Expand Down
3 changes: 3 additions & 0 deletions spec/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
require_relative '../lib/asciidoctor-diagram/graphviz/extension'
require_relative '../lib/asciidoctor-diagram/plantuml/extension'
require_relative '../lib/asciidoctor-diagram/shaape/extension'
require_relative '../lib/asciidoctor-diagram/wavedrom/extension'

module Asciidoctor
class AbstractBlock
Expand All @@ -30,6 +31,8 @@ def find(&block)
end

RSpec.configure do |c|
c.filter_run_excluding :broken => true

TEST_DIR = File.expand_path('testing')

c.before(:suite) do
Expand Down
Loading