Skip to content

Commit

Permalink
resolves #261 use the new syntax highlighter API introduced in Asciid…
Browse files Browse the repository at this point in the history
…octor 2.0.0
  • Loading branch information
ggrossetie committed Dec 10, 2019
1 parent 862588a commit 43ef818
Show file tree
Hide file tree
Showing 16 changed files with 455 additions and 205 deletions.
41 changes: 41 additions & 0 deletions examples/source-rouge.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// .source-rouge
// Demonstration of source highlighting with Rouge
// :include: //div[@class="slides"]
// :header_footer:
= Source Code with Rouge
:icons: font
:source-highlighter: rouge
:rouge-style: monokai

== Requirements

WARNING: This will not work from Asciidoctor.js

[NOTE]
====
For this to work. You need to add:
gem 'rouge'
to your `Gemfile` and re-run:
bundle install
====

== Use the Source

[source, rust]
----
fn main() {
println!("Hello World!");
}
----

== Stretch the Source

[source, rust, role="stretch"]
----
fn main() {
println!("Hello stretched World!");
}
----
2 changes: 2 additions & 0 deletions lib/asciidoctor-revealjs.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
if RUBY_ENGINE == 'opal'
require 'asciidoctor-revealjs/converter'
require 'asciidoctor-revealjs/version'
require 'asciidoctor-revealjs/highlightjs'
else
require 'asciidoctor' unless defined? Asciidoctor::Converter
require_relative 'asciidoctor-revealjs/converter'
require_relative 'asciidoctor-revealjs/version'
require_relative 'asciidoctor-revealjs/highlightjs'
end
41 changes: 41 additions & 0 deletions lib/asciidoctor-revealjs/highlightjs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true
module Asciidoctor
module Revealjs
module SyntaxHighlighter
# Override the built-in highlight.js syntax highlighter
class HighlightJsAdapter < Asciidoctor::SyntaxHighlighter::Base
register_for 'highlightjs', 'highlight.js'

def initialize *args
super
@name = @pre_class = 'highlightjs'
end

def format node, lang, opts
super node, lang, (opts.merge transform: proc { |_, code|
code['class'] = %(language-#{lang || 'none'} hljs)
code['data-noescape'] = true
})
end

def docinfo? location
location == :footer
end

def docinfo location, doc, opts
if RUBY_ENGINE == 'opal' && JAVASCRIPT_PLATFORM == 'node'
revealjsdir = (doc.attr :revealjsdir, 'node_modules/reveal.js')
else
revealjsdir = (doc.attr :revealjsdir, 'reveal.js')
end
if doc.attr? 'highlightjs-theme'
theme_href = doc.attr 'highlightjs-theme'
else
theme_href = "#{revealjsdir}/lib/css/zenburn.css"
end
%(<link rel="stylesheet" href="#{theme_href}"#{opts[:self_closing_tag_slash]}>)
end
end
end
end
end
12 changes: 12 additions & 0 deletions templates/asciidoctor_revealjs.css.slim
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ css:
float: right;
}

.reveal .listingblock.stretch > .content {
height: 100%;
}

.reveal .listingblock.stretch > .content > pre {
height: 100%;
}

.reveal .listingblock.stretch > .content > pre > code {
height: 100%;
}

/* callouts */
.conum[data-value] {display:inline-block;color:#fff!important;background-color:rgba(50,150,50,.8);-webkit-border-radius:100px;border-radius:100px;text-align:center;font-size:.75em;width:1.67em;height:1.67em;line-height:1.67em;font-family:"Open Sans","DejaVu Sans",sans-serif;font-style:normal;font-weight:bold}
.conum[data-value] *{color:#fff!important}
Expand Down
26 changes: 8 additions & 18 deletions templates/document.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ html lang=(attr :lang, 'en' unless attr? :nolang)
- [:description, :keywords, :author, :copyright].each do |key|
- if attr? key
meta name=key content=(attr key)
- linkcss = (attr? 'linkcss')
title=(doctitle sanitize: true, use_fallback: true)
meta content="yes" name="apple-mobile-web-app-capable"
meta content="black-translucent" name="apple-mobile-web-app-status-bar-style"
Expand Down Expand Up @@ -46,24 +47,10 @@ html lang=(attr :lang, 'en' unless attr? :nolang)
TeX: {#{eqnums_opt}}
});
script src='#{cdn_base}/mathjax/2.4.0/MathJax.js?config=TeX-MML-AM_HTMLorMML'
- case document.attr 'source-highlighter'
- when 'coderay'
- if (attr 'coderay-css', 'class') == 'class'
- if @safe >= Asciidoctor::SafeMode::SECURE || (attr? :linkcss)
link rel='stylesheet' href=normalize_web_path('asciidoctor-coderay.css', (attr :stylesdir, ''))
- else
style=Asciidoctor::Stylesheets.instance.coderay_stylesheet_data
- when 'pygments'
- if (attr 'pygments-css', 'class') == 'class'
- if @safe >= Asciidoctor::SafeMode::SECURE || (attr? :linkcss)
link rel='stylesheet' href=normalize_web_path('asciidoctor-pygments.css', (attr :stylesdir, ''))
- else
style=Asciidoctor::Stylesheets.instance.pygments_stylesheet_data(attr 'pygments-style')
/ For syntax highlighting
- if attr? 'highlightjs-theme'
link href=(attr 'highlightjs-theme') rel="stylesheet"
- else
link href="#{revealjsdir}/lib/css/zenburn.css" rel="stylesheet"

- syntax_hl = self.syntax_highlighter
- if syntax_hl && (syntax_hl.docinfo? :head)
=syntax_hl.docinfo :head, self, cdn_base_url: cdn_base, linkcss: linkcss, self_closing_tag_slash: '/'
/ If the query includes 'print-pdf', use the PDF print sheet
javascript:
var link = document.createElement( 'link' );
Expand Down Expand Up @@ -250,5 +237,8 @@ html lang=(attr :lang, 'en' unless attr? :nolang)
#{(attr? 'revealjs_plugins_configuration') ? File.read(attr('revealjs_plugins_configuration', '')) : ""}
});
- if syntax_hl && (syntax_hl.docinfo? :footer)
=syntax_hl.docinfo :footer, self, cdn_base_url: cdn_base, linkcss: linkcss, self_closing_tag_slash: '/'
- unless (docinfo_content = (docinfo :footer, '.html')).empty?
=docinfo_content
52 changes: 18 additions & 34 deletions templates/listing.html.slim
Original file line number Diff line number Diff line change
@@ -1,35 +1,19 @@
- if title?
.title=captioned_title
- nowrap = !(@document.attr? :prewrap) || (option? 'nowrap')
/ implicit listing blocks and source blocks are rendered as code
/ explicit listing blocks stay listing
- if @style == 'source' || (@style == 'listing' && attributes[1] != 'listing')
- language = attr :language
- code_class = language ? [language, "language-#{language}"] : nil
- pre_class = ['highlight']
- pre_lang = nil
- code_noescape = false
- case document.attr 'source-highlighter'
- when 'coderay'
- pre_class = ['CodeRay']
- when 'pygments'
- pre_class = ['pygments','highlight']
- when 'prettify'
- pre_class = ['prettyprint']
- pre_class << 'linenums' if attr? :linenums
- pre_class << language if language
- pre_class << "language-#{language}" if language
- code_class = nil
- when 'html-pipeline'
- pre_lang = language
- pre_class = code_class = nil
- nowrap = false
- when 'highlightjs', 'highlight.js'
- code_noescape=true
- pre_class << 'nowrap' if nowrap
- pre_class << 'listingblock'
- pre_class << role if role
pre class=pre_class lang=pre_lang id=@id
code data-noescape=code_noescape class=code_class =content
- nowrap = (option? 'nowrap') || !(document.attr? 'prewrap')
- syntax_hl = document.syntax_highlighter
- if @style == 'source'
- lang = attr :language
- if syntax_hl
- doc_attrs = document.attributes
- css_mode = (doc_attrs[%(#{syntax_hl.name}-css)] || :class).to_sym
- style = doc_attrs[%(#{syntax_hl.name}-style)]
- opts = syntax_hl.highlight? ? { css_mode: css_mode, style: style } : {}
- opts[:nowrap] = nowrap
- else
- pre_open = %(<pre class="highlight#{nowrap ? ' nowrap' : ''}"><code#{lang ? %[ class="language-#{lang}" data-lang="#{lang}"] : ''}>)
- pre_close = '</code></pre>'
- else
pre class=(nowrap ? 'nowrap' : nil) =content
- pre_open = %(<pre#{nowrap ? ' class="nowrap"' : ''}>)
- pre_close = '</pre>'
- id_attribute = id ? %( id="#{id}") : ''
- title_element = title? ? %(<div class="title">#{captioned_title}</div>\n) : ''
=%(<div#{id_attribute} class="listingblock#{(role = self.role) ? " #{role}" : ''}">#{title_element}<div class="content">#{syntax_hl ? (syntax_hl.format self, lang, opts) : pre_open + (content || '') + pre_close}</div></div>)
18 changes: 15 additions & 3 deletions test/doctest/colist.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<!-- .basic -->
<pre class="highlight listingblock"><code class="ruby language-ruby">require 'sinatra' <b>(1)</b>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-ruby" data-lang="ruby">require 'sinatra' <b>(1)</b>

get '/hi' do <b>(2)</b>
"Hello World!" <b>(3)</b>
end</code></pre>
</div>
</div>
<div class="colist arabic">
<ol>
<li>
Expand All @@ -19,11 +23,15 @@
</div>

<!-- .with-title -->
<pre class="highlight listingblock"><code class="ruby language-ruby">require 'sinatra' <b>(1)</b>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-ruby" data-lang="ruby">require 'sinatra' <b>(1)</b>

get '/hi' do <b>(2)</b>
"Hello World!" <b>(3)</b>
end</code></pre>
</div>
</div>
<div class="colist arabic">
<div class="title">Description</div>
<ol>
Expand All @@ -40,11 +48,15 @@
</div>

<!-- .with-id-and-role -->
<pre class="highlight listingblock"><code class="ruby language-ruby">require 'sinatra' <b>(1)</b>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-ruby" data-lang="ruby">require 'sinatra' <b>(1)</b>

get '/hi' do <b>(2)</b>
"Hello World!" <b>(3)</b>
end</code></pre>
</div>
</div>
<div class="colist arabic sinatra" id="call">
<ol>
<li>
Expand Down
Loading

0 comments on commit 43ef818

Please sign in to comment.