templates/helpers.rb in asciidoctor-revealjs-4.1.0 vs templates/helpers.rb in asciidoctor-revealjs-5.0.0.rc1

- old
+ new

@@ -2,10 +2,12 @@ # This helper file borrows from the Bespoke converter # https://github.com/asciidoctor/asciidoctor-bespoke require 'asciidoctor' end +require 'json' + # This module gets mixed in to every node (the context of the template) at the # time the node is being converted. The properties and methods in this module # effectively become direct members of the template. module Slim::Helpers @@ -191,19 +193,13 @@ end end def revealjs_dependencies(document, node, revealjsdir) dependencies = [] - dependencies << "{ src: '#{revealjsdir}/plugin/zoom-js/zoom.js', async: true }" unless (node.attr? 'revealjs_plugin_zoom', 'disabled') - dependencies << "{ src: '#{revealjsdir}/plugin/notes/notes.js', async: true }" unless (node.attr? 'revealjs_plugin_notes', 'disabled') - dependencies << "{ src: '#{revealjsdir}/plugin/markdown/marked.js', async: true }" if (node.attr? 'revealjs_plugin_marked', 'enabled') - dependencies << "{ src: '#{revealjsdir}/plugin/markdown/markdown.js', async: true }" if (node.attr? 'revealjs_plugin_markdown', 'enabled') - if (node.attr? 'revealjs_plugins') && - !(revealjs_plugins_file = (node.attr 'revealjs_plugins', '').strip).empty? && - !(revealjs_plugins_content = (File.read revealjs_plugins_file).strip).empty? - dependencies << revealjs_plugins_content - end + dependencies << "{ src: '#{revealjsdir}/plugin/zoom/zoom.js', async: true, callback: function () { Reveal.registerPlugin(RevealZoom) } }" unless (node.attr? 'revealjs_plugin_zoom', 'disabled') + dependencies << "{ src: '#{revealjsdir}/plugin/notes/notes.js', async: true, callback: function () { Reveal.registerPlugin(RevealNotes) } }" unless (node.attr? 'revealjs_plugin_notes', 'disabled') + dependencies << "{ src: '#{revealjsdir}/plugin/search/search.js', async: true, callback: function () { Reveal.registerPlugin(RevealSearch) } }" if (node.attr? 'revealjs_plugin_search', 'enabled') dependencies.join(",\n ") end # Between delimiters (--) is code taken from asciidoctor-bespoke 1.0.0.alpha.1 @@ -276,9 +272,87 @@ def yield_content key, opts = {} if (defined? @content) && (blks = (opts.fetch :drain, true) ? (@content.delete key) : @content[key]) blks.map {|b| b.call }.join end nil + end + + # Copied from asciidoctor/lib/asciidoctor/converter/semantic-html5.rb which is not yet shipped + # @todo remove this code when the new converter becomes available in the main gem + def generate_authors node + return if node.authors.empty? + + if node.authors.length == 1 + %(<p class="byline"> +#{format_author node, node.authors.first} +</p>) + else + result = ['<ul class="byline">'] + node.authors.each do |author| + result << "<li>#{format_author node, author}</li>" + end + result << '</ul>' + result.join Asciidoctor::LF + end + end + + # Copied from asciidoctor/lib/asciidoctor/converter/semantic-html5.rb which is not yet shipped + # @todo remove this code when the new converter becomes available in the main gem + def format_author node, author + in_context 'author' do + %(<span class="author">#{node.sub_replacements author.name}#{author.email ? %( #{node.sub_macros author.email}) : ''}</span>) + end + end + + # Copied from asciidoctor/lib/asciidoctor/converter/semantic-html5.rb which is not yet shipped + # @todo remove this code when the new converter becomes available in the main gem + def in_context name + (@convert_context ||= []).push name + result = yield + @convert_context.pop + result + end + + STEM_EQNUMS_AMS = 'ams' + STEM_EQNUMS_NONE = 'none' + STEM_EQNUMS_VALID_VALUES = [ + STEM_EQNUMS_NONE, + STEM_EQNUMS_AMS, + 'all' + ] + + MATHJAX_VERSION = '3.2.0' + + # Generate the Mathjax markup to process STEM expressions + # @param cdn_base [String] + # @return [String] + def generate_stem(cdn_base) + if attr?(:stem) + eqnums_val = attr('eqnums', STEM_EQNUMS_NONE).downcase + unless STEM_EQNUMS_VALID_VALUES.include?(eqnums_val) + eqnums_val = STEM_EQNUMS_AMS + end + mathjax_configuration = { + tex: { + inlineMath: [Asciidoctor::INLINE_MATH_DELIMITERS[:latexmath]], + displayMath: [Asciidoctor::BLOCK_MATH_DELIMITERS[:latexmath]], + processEscapes: false, + tags: eqnums_val, + }, + options: { + ignoreHtmlClass: 'nostem|nolatexmath' + }, + asciimath: { + delimiters: [Asciidoctor::BLOCK_MATH_DELIMITERS[:asciimath]], + }, + loader: { + load: ['input/asciimath', 'output/chtml', 'ui/menu'] + } + } + mathjaxdir = attr('mathjaxdir', "#{cdn_base}/mathjax/#{MATHJAX_VERSION}/es5") + %(<script>window.MathJax = #{JSON.generate(mathjax_configuration)};</script>) + + %(<script async src="#{mathjaxdir}/tex-mml-chtml.js"></script>) + end end #-- end # More custom functions can be added in another namespace if required