Module: Asciidoctor::Html5s::Converter::Helpers

Defined in:
lib/asciidoctor/html5s/converter.rb

Overview

Add custom functions to this module that you want to use in your Slim templates. Within the template you can invoke them as top-level functions just like in Haml.

Constant Summary collapse

FONT_AWESOME_URI =

URIs of external assets.

'//cdn.jsdelivr.net/npm/[email protected]/css/font-awesome.min.css'
HIGHLIGHTJS_BASE_URI =
'//cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/'
KATEX_CSS_URI =
'//cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css'
KATEX_JS_URI =
'//cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.js'
DEFAULT_HIGHLIGHTJS_THEME =

Defaults

'github'
DEFAULT_LANG =
'en'
DEFAULT_SECTNUMLEVELS =
3
DEFAULT_TOCLEVELS =
2
CURLY_QUOTES =
[
  [%w[af en eo ga hi ia id ko mt th tr zh], ['‘', '’', '“', '”']],  # ‘…’ “…”
  [%w[bs fi sv], ['’', '’', '”', '”']],  # ’…’ ”…”
  [%w[cs da de is lt sl sk sr], ['‚', '‘', '„', '“']],  # ‚…‘ „…“
  [%w[nl], ['‚', '’', '„', '”']],  # ‚…’ „…”
  [%w[hu pl ro], ['«', '»', '„', '”']],  # «…» „…”
].reduce({}) do |hsh, (langs, codes)|
  langs.each { |lang| hsh[lang] = codes }
  hsh
end
KATEX_RENDER_CODE =
<<-JS.gsub(/\s+/, ' ')
  document.addEventListener("DOMContentLoaded", function() {
    var elements = document.getElementsByClassName("math");
    for (var i = 0; i < elements.length; i++) {
      var el = elements[i];
      if (el.getAttribute("data-lang") !== "tex") {
        continue;
      }
      katex.render(el.textContent.slice(2, -2), el, {
        "displayMode": el.nodeName.toUpperCase() !== "SPAN",
        "throwOnError": false,
      });
    }
  });
JS
VOID_ELEMENTS =
%w(area base br col command embed hr img input keygen link
meta param source track wbr)

Instance Method Summary collapse

Instance Method Details

#abstract_allowed?Boolean

Returns true if an abstract block is allowed in this document type, otherwise prints warning and returns false.

Returns:

  • (Boolean)


498
499
500
501
502
503
504
# File 'lib/asciidoctor/html5s/converter.rb', line 498

def abstract_allowed?
  if result = (parent == document && document.doctype == 'book')
    log.warn 'asciidoctor: WARNING: abstract block cannot be used in a document
  without a title when doctype is book. Excluding block content.'
  end
  !result
end

#admonition_ariaString?

Returns WAI-ARIA role of this admonition.

Returns:

  • (String, nil)

    WAI-ARIA role of this admonition.



421
422
423
424
425
426
427
428
429
430
# File 'lib/asciidoctor/html5s/converter.rb', line 421

def admonition_aria
  case attr(:name)
  when 'note'
    'note'  # https://www.w3.org/TR/wai-aria/roles#note
  when 'tip'
    'doc-tip'  # https://www.w3.org/TR/dpub-aria-1.0/#doc-tip
  when 'caution', 'important', 'warning'
    'doc-notice'  # https://www.w3.org/TR/dpub-aria-1.0/#doc-notice
  end
end

#admonition_aside?Boolean

Returns should be this admonition wrapped in aside element?.

Returns:

  • (Boolean)

    should be this admonition wrapped in aside element?



415
416
417
# File 'lib/asciidoctor/html5s/converter.rb', line 415

def admonition_aside?
  %w[note tip].include? attr(:name)
end

#autowidth?Boolean


block_table

Returns:

  • (Boolean)


521
522
523
# File 'lib/asciidoctor/html5s/converter.rb', line 521

def autowidth?
  option? :autowidth
end

#bibref_textString?

Returns text of the bibref anchor, or nil if not found.

Returns:

  • (String, nil)

    text of the bibref anchor, or nil if not found.



700
701
702
703
704
705
706
707
708
709
710
# File 'lib/asciidoctor/html5s/converter.rb', line 700

def bibref_text
  if document.respond_to? :catalog  # Asciidoctor >=1.5.6
    if ::Asciidoctor::VERSION[0] == '1'
      text
    else  # Asciidoctor >= 2.0.0
      "[#{reftext || id}]"
    end
  else
    "[#{target}]"
  end
end

#block_with_caption(position = :bottom, attrs = {}, &block) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/asciidoctor/html5s/converter.rb', line 224

def block_with_caption(position = :bottom, attrs = {}, &block)
  if (klass = attrs[:class]).is_a? String
    klass = klass.split(' ')
  end
  attrs[:class] = [klass, role].flatten.uniq
  attrs[:id] = id

  if title.nil_or_empty?
    html_tag :div, attrs, yield
  else
    html_tag :figure, attrs do
      ary = [yield, html_tag(:figcaption) { captioned_title }]
      ary.reverse! if position == :top
      ary.compact.join("\n")
    end
  end
end

#block_with_title(attrs = {}, title = @title) { ... } ⇒ String

Wraps a block in a div element with the specified class and optionally the node’s id and role(s). If the node’s title is not empty, then a nested div with the class “title” and the title’s content is added as well.

Examples:

When @id, @role and @title attributes are set.

= block_with_title :class=>['quote-block', 'center']
  blockquote =content

<section id="myid" class="quote-block center myrole1 myrole2">
  <h6>Block Title</h6>
  <blockquote>Lorem ipsum</blockquote>
</section>

When @id, @role and @title attributes are empty.

= block_with_title :class=>'quote-block center', :style=>style_value(float: 'left')
  blockquote =content

<div class="quote-block center" style="float: left;">
  <blockquote>Lorem ipsum</blockquote>
</div>

When shorthand style for class attribute is used.

= block_with_title 'quote-block center'
  blockquote =content

<div class="quote-block center">
  <blockquote>Lorem ipsum</blockquote>
</div>

Parameters:

  • attrs (Hash, String) (defaults to: {})

    the tag’s attributes as Hash), or the tag’s class if it’s not a Hash.

  • title (String, nil) (defaults to: @title)

    the title.

Yields:

  • The block of Slim/HTML code within the tag (optional).

Returns:

  • (String)

    a rendered HTML fragment.



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/asciidoctor/html5s/converter.rb', line 205

def block_with_title(attrs = {}, title = @title, &block)
  if (klass = attrs[:class]).is_a? String
    klass = klass.split(' ')
  end
  attrs[:class] = [klass, role].flatten.uniq
  attrs[:id] = id

  if title.nil_or_empty?
    # XXX quick hack
    nested = is_a?(::Asciidoctor::List) &&
        (parent.is_a?(::Asciidoctor::ListItem) || parent.is_a?(::Asciidoctor::List))
    html_tag_if !nested, :div, attrs, yield
  else
    html_tag :section, attrs do
      [html_tag(:h6, {class: 'block-title'}, title), yield].join("\n")
    end
  end
end

#callout_listAsciidoctor::List?

Returns the callout list attached to this listing node, or nil if none.

Note: This variable is set by extension AttachedColistTreeprocessor.

Returns:

  • (Asciidoctor::List, nil)


483
484
485
# File 'lib/asciidoctor/html5s/converter.rb', line 483

def callout_list
  @html5s_colist
end

#capture(&block) ⇒ Object

Captures the given block for later yield.

Examples:

Basic capture usage.

- capture
  img src=image_uri
- if title?
  figure.image
    - yield_capture
    figcaption =captioned_title
- else
  - yield_capture

Capture with passing parameters.

- capture do |id|
  img src=image_uri
- if title?
  figure id=@id
    - yield_capture
    figcaption =caption
- else
  - yield_capture @id

See Also:



90
91
92
93
# File 'lib/asciidoctor/html5s/converter.rb', line 90

def capture(&block)
  @_html5s_capture = block
  nil
end

#delimit_stem(equation, type) ⇒ String

Delimite the given equation as a STEM of the specified type.

Note: This is not needed nor used for KaTeX, but keep this for the case user wants to use a different method.

Parameters:

  • equation (String)

    the equation to delimite.

  • type (#to_sym)

    the type of the STEM renderer (latexmath, or asciimath).

Returns:

  • (String)

    the delimited equation.



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/asciidoctor/html5s/converter.rb', line 252

def delimit_stem(equation, type)
  if (@_html5s_stem_type ||= document.attr('html5s-force-stem-type'))
    type = @_html5s_stem_type
  end

  if is_a? ::Asciidoctor::Block
    open, close = ::Asciidoctor::BLOCK_MATH_DELIMITERS[type.to_sym]
  else
    open, close = ::Asciidoctor::INLINE_MATH_DELIMITERS[type.to_sym]
  end

  if !equation.start_with?(open) || !equation.end_with?(close)
    equation = [open, equation, close].join
  end
  equation
end

#double_quoted(text) ⇒ String

Returns quoted text.

Parameters:

  • text (String)

    the text to wrap in double quotes.

Returns:

  • (String)

    quoted text.



731
732
733
734
# File 'lib/asciidoctor/html5s/converter.rb', line 731

def double_quoted(text)
  quotes = CURLY_QUOTES[attr(:lang, DEFAULT_LANG, true)]
  "#{quotes[2]}#{text}#{quotes[3]}"
end

#footnote_id(index = local_attr(:index)) ⇒ String

Returns footnote id to be used in a link.

Parameters:

  • index (Integer) (defaults to: local_attr(:index))

    the footnote’s index.

Returns:

  • (String)

    footnote id to be used in a link.



352
353
354
# File 'lib/asciidoctor/html5s/converter.rb', line 352

def footnote_id(index = local_attr(:index))
  "_footnote_#{index}"
end

#footnoteref_id(index = local_attr(:index)) ⇒ String

Returns footnoteref id to be used in a link.

Parameters:

  • index (Integer) (defaults to: local_attr(:index))

    the footnote’s index.

Returns:

  • (String)

    footnoteref id to be used in a link.



359
360
361
# File 'lib/asciidoctor/html5s/converter.rb', line 359

def footnoteref_id(index = local_attr(:index))
  "_footnoteref_#{index}"
end

#highlighterString

Returns a canonical name of the source-highlighter to be used as a style class.

Returns:

  • (String)

    a canonical name of the source-highlighter to be used as a style class.



467
468
469
470
471
472
473
474
# File 'lib/asciidoctor/html5s/converter.rb', line 467

def highlighter
  @_html5s_highlighter ||=
    case (highlighter = document.attr('source-highlighter'))
    when 'coderay'; 'CodeRay'
    when 'highlight.js'; 'highlightjs'
    else highlighter
    end
end

#html_meta_if(name, content) ⇒ String?

Returns HTML meta tag if the given content is not nil.

Parameters:

  • name (#to_s)

    the name for the metadata.

  • content (#to_s, nil)

    the value of the metadata, or nil.

Returns:

  • (String, nil)

    the meta tag, or nil if the content is nil.



600
601
602
# File 'lib/asciidoctor/html5s/converter.rb', line 600

def html_meta_if(name, content)
  %(<meta name="#{name}" content="#{content}">) if content
end

#html_tag(name, attributes = {}, content = nil) { ... } ⇒ String

Creates an HTML tag with the given name and optionally attributes. Can take a block that will run between the opening and closing tags.

Parameters:

  • name (#to_s)

    the name of the tag.

  • attributes (Hash) (defaults to: {})

    (default: {})

  • content (#to_s) (defaults to: nil)

    the content; nil to call the block. (default: nil).

Yields:

  • The block of Slim/HTML code within the tag (optional).

Returns:

  • (String)

    a rendered HTML element.



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/asciidoctor/html5s/converter.rb', line 115

def html_tag(name, attributes = {}, content = nil)
  attrs = attributes.inject([]) do |attrs, (k, v)|
    next attrs if !v || v.nil_or_empty?
    v = v.compact.join(' ') if v.is_a? Array
    attrs << (v == true ? k : %(#{k}="#{v}"))
  end
  attrs_str = attrs.empty? ? '' : ' ' + attrs.join(' ')

  if VOID_ELEMENTS.include? name.to_s
    %(<#{name}#{attrs_str}>)
  else
    content ||= yield if block_given?
    %(<#{name}#{attrs_str}>#{content}</#{name}>)
  end
end

#html_tag_if(condition, name, attributes = {}, content = nil) { ... } ⇒ String

Conditionally wraps a block in an element. If condition is true then it renders the specified tag with optional attributes and the given block inside, otherwise it just renders the block.

For example:

= html_tag_if link?, 'a', {class: 'image', href: (attr :link)}
  img src='./img/tux.png'

will produce:

<a href="http://example.org" class="image">
  <img src="./img/tux.png">
</a>

if link? is truthy, and just

<img src="./img/tux.png">

otherwise.

Parameters:

  • condition (Boolean)

    the condition to test to determine whether to render the enclosing tag.

  • name (#to_s)

    the name of the tag.

  • attributes (Hash) (defaults to: {})

    (default: {})

  • content (#to_s) (defaults to: nil)

    the content; nil to call the block. (default: nil).

Yields:

  • The block of Slim/HTML code within the tag (optional).

Returns:

  • (String)

    a rendered HTML fragment.



161
162
163
164
165
166
167
# File 'lib/asciidoctor/html5s/converter.rb', line 161

def html_tag_if(condition, name, attributes = {}, content = nil, &block)
  if condition
    html_tag name, attributes, content, &block
  else
    content || yield
  end
end

#icon_fa_classesArray

Returns style classes for a Font Awesome icon.

Returns:

  • (Array)

    style classes for a Font Awesome icon.



717
718
719
720
721
722
723
# File 'lib/asciidoctor/html5s/converter.rb', line 717

def icon_fa_classes
  [ "fa fa-#{target}",
    ("fa-#{attr :size}" if attr? :size),
    ("fa-rotate-#{attr :rotate}" if attr? :rotate),
    ("fa-flip-#{attr :flip}" if attr? :flip)
  ].compact
end

Returns an URL for the image’s link.

Returns:

  • (String, nil)

    an URL for the image’s link.



438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'lib/asciidoctor/html5s/converter.rb', line 438

def image_link
  @_html5s_image_link ||=
    case (link = attr(:link))
    when 'none', 'false'
      return
    when 'self'
      image_uri(attr(:target))
    when nil, ''
      image_uri(attr(:target)) if document.attr?('html5s-image-default-link', 'self')
    else
      link
    end
end

Returns a label/title of the image link.

Returns:

  • (String, nil)

    a label/title of the image link.



454
455
456
457
458
# File 'lib/asciidoctor/html5s/converter.rb', line 454

def image_link_label
  if image_uri(attr(:target)) == image_link
    document.attr('html5s-image-self-link-label', 'Open the image in full size')
  end
end


405
406
407
# File 'lib/asciidoctor/html5s/converter.rb', line 405

def link_rel
  'noopener' if option?('noopener') || attr(:window) == '_blank'
end

#local_attr(name, default_val = nil) ⇒ Object

Gets the value of the specified attribute in this node.

This is just an alias for attr method with disabled inherit to make it more clear.

Parameters:

  • name (String, Symbol)

    the name of the attribute to lookup.

  • default_val (defaults to: nil)

    the value to return if the attribute is not found.

Returns:

  • value of the attribute or default_val if not found.



329
330
331
# File 'lib/asciidoctor/html5s/converter.rb', line 329

def local_attr(name, default_val = nil)
  attr(name, default_val, false)
end

#local_attr?(name, expect_val = nil) ⇒ Boolean

Checks if the attribute is defined on this node, optionally performing a comparison of its value if expect_val is not nil.

This is just an alias for attr? method with disabled inherit to make it more clear.

Parameters:

  • name (String, Symbol)

    the name of the attribute to lookup.

  • default_val

    the expected value of the attribute.

Returns:

  • (Boolean)

    whether the attribute exists and, if expect_val is specified, whether the value of the attribute matches the expect_val.



345
346
347
# File 'lib/asciidoctor/html5s/converter.rb', line 345

def local_attr?(name, expect_val = nil)
  attr?(name, expect_val, false)
end

#logLogger

Returns:

  • (Logger)


62
63
64
# File 'lib/asciidoctor/html5s/converter.rb', line 62

def log
  @_html5s_logger ||= ::Asciidoctor::Html5s::Logging.default_logger
end

#nowrap?Boolean

Returns:

  • (Boolean)


363
364
365
# File 'lib/asciidoctor/html5s/converter.rb', line 363

def nowrap?
  'nowrap' if !document.attr?(:prewrap) || option?('nowrap')
end

#partintro_allowed?Boolean

Returns true if a partintro block is allowed in this context, otherwise prints warning and returns false.

Returns:

  • (Boolean)


509
510
511
512
513
514
515
# File 'lib/asciidoctor/html5s/converter.rb', line 509

def partintro_allowed?
  if result = (level != 0 || parent.context != :section || document.doctype != 'book')
    log.warn "asciidoctor: ERROR: partintro block can only be used when doctype
  is book and it's a child of a book part. Excluding block content."
  end
  !result
end


367
368
369
370
# File 'lib/asciidoctor/html5s/converter.rb', line 367

def print_item_content(item)
  wrap = item.blocks? && !item.blocks.all? { |b| b.is_a? ::Asciidoctor::List }
  [ (html_tag_if(wrap, :p) { item.text } if item.text?), item.content ].join
end

#revdate_isoString?

Returns the revision date in ISO 8601, or nil if not available or in invalid format.

Returns:

  • (String, nil)

    the revision date in ISO 8601, or nil if not available or in invalid format.



587
588
589
590
591
# File 'lib/asciidoctor/html5s/converter.rb', line 587

def revdate_iso
  ::Date.parse(revdate).iso8601 if defined? ::Date
rescue ArgumentError
  nil
end

#section_level(sec = self) ⇒ Integer

Returns corrected section level.

Parameters:

  • sec (Asciidoctor::Section) (defaults to: self)

    the section node (default: self).

Returns:

  • (Integer)


378
379
380
# File 'lib/asciidoctor/html5s/converter.rb', line 378

def section_level(sec = self)
  (sec.level == 0 && sec.special) ? 1 : sec.level
end

#section_title(sec = self) ⇒ String

Returns the captioned section’s title, optionally numbered.

Parameters:

  • sec (Asciidoctor::Section) (defaults to: self)

    the section node (default: self).

Returns:

  • (String)


388
389
390
391
392
393
394
395
396
# File 'lib/asciidoctor/html5s/converter.rb', line 388

def section_title(sec = self)
  sectnumlevels = document.attr(:sectnumlevels, DEFAULT_SECTNUMLEVELS).to_i

  if sec.numbered && !sec.caption && sec.level <= sectnumlevels
    [sec.sectnum, sec.captioned_title].join(' ')
  else
    sec.captioned_title
  end
end

#single_quoted(text) ⇒ String

Returns quoted text.

Parameters:

  • text (String)

    the text to wrap in single quotes.

Returns:

  • (String)

    quoted text.



738
739
740
741
# File 'lib/asciidoctor/html5s/converter.rb', line 738

def single_quoted(text)
  quotes = CURLY_QUOTES[attr(:lang, DEFAULT_LANG, true)]
  "#{quotes[0]}#{text}#{quotes[1]}"
end

#source_langObject



487
488
489
# File 'lib/asciidoctor/html5s/converter.rb', line 487

def source_lang
  local_attr :language, false
end

#spread?Boolean

Returns:

  • (Boolean)


525
526
527
528
529
# File 'lib/asciidoctor/html5s/converter.rb', line 525

def spread?
  if !autowidth? || local_attr?('width')
    'spread' if attr? :tablepcwidth, 100
  end
end

#stem_langString

Returns language of STEM block or inline node (tex or asciimath).

Returns:

  • (String)

    language of STEM block or inline node (tex or asciimath).



400
401
402
403
# File 'lib/asciidoctor/html5s/converter.rb', line 400

def stem_lang
  value = (inline? ? type : style).to_s
  value == 'latexmath' ? 'tex' : value
end

#style_value(declarations) ⇒ String?

Formats the given hash as CSS declarations for an inline style.

Examples:

style_value(text_align: 'right', float: 'left')
=> "text-align: right; float: left;"

style_value(text_align: nil, float: 'left')
=> "float: left;"

style_value(width: [90, '%'], height: '50px')
=> "width: 90%; height: 50px;"

style_value(width: ['120px', 'px'])
=> "width: 90px;"

style_value(width: [nil, 'px'])
=> nil

Parameters:

  • declarations (Hash)

Returns:

  • (String, nil)


291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/asciidoctor/html5s/converter.rb', line 291

def style_value(declarations)
  decls = []

  declarations.each do |prop, value|
    next if value.nil?

    if value.is_a? Array
      value, unit = value
      next if value.nil?
      value = value.to_s + unit unless value.end_with? unit
    end
    prop = prop.to_s.gsub('_', '-')
    decls << "#{prop}: #{value}"
  end

  decls.empty? ? nil : decls.join('; ') + ';'
end

#styles_and_scriptsObject

Returns formatted style/link and script tags for header.



605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
# File 'lib/asciidoctor/html5s/converter.rb', line 605

def styles_and_scripts
  scripts = []
  styles = []
  tags = []

  stylesheet = attr :stylesheet
  stylesdir = attr :stylesdir, ''
  default_style = ::Asciidoctor::DEFAULT_STYLESHEET_KEYS.include? stylesheet
  linkcss = attr?(:linkcss) || safe >= ::Asciidoctor::SafeMode::SECURE
  ss = ::Asciidoctor::Stylesheets.instance

  if linkcss
    path = default_style ? ::Asciidoctor::DEFAULT_STYLESHEET_NAME : stylesheet
    styles << { href: [stylesdir, path] }
  elsif default_style
    styles << { text: ss.primary_stylesheet_data }
  else
    styles << { text: read_asset(normalize_system_path(stylesheet, stylesdir), true) }
  end

  if attr? :icons, 'font'
    if attr? 'iconfont-remote'
      styles << { href: attr('iconfont-cdn', FONT_AWESOME_URI) }
    else
      styles << { href: [stylesdir, "#{attr 'iconfont-name', 'font-awesome'}.css"] }
    end
  end

  if attr? 'stem'
    styles << { href: KATEX_CSS_URI }
    scripts << { src: KATEX_JS_URI }
    scripts << { text: KATEX_RENDER_CODE }
  end

  case attr 'source-highlighter'
  when 'coderay'
    if attr('coderay-css', 'class') == 'class'
      if linkcss
        styles << { href: [stylesdir, ss.coderay_stylesheet_name] }
      else
        styles << { text: ss.coderay_stylesheet_data }
      end
    end

  when 'highlightjs'
    hjs_base = attr :highlightjsdir, HIGHLIGHTJS_BASE_URI
    hjs_theme = attr 'highlightjs-theme', DEFAULT_HIGHLIGHTJS_THEME

    scripts << { src: [hjs_base, 'highlight.min.js'] }
    scripts << { text: 'hljs.initHighlightingOnLoad()' }
    styles  << { href: [hjs_base, "styles/#{hjs_theme}.min.css"] }
  end

  styles.each do |item|
    if item.key?(:text)
      tags << html_tag(:style) { item[:text] }
    else
      tags << html_tag(:link, rel: 'stylesheet', href: urlize(*item[:href]))
    end
  end

  scripts.each do |item|
    if item.key? :text
      tags << html_tag(:script, type: item[:type]) { item[:text] }
    else
      tags << html_tag(:script, type: item[:type], src: urlize(*item[:src]))
    end
  end

  tags.join("\n")
end

#url_query(params) ⇒ Object

Formats URL query parameters.



571
572
573
574
575
576
577
578
# File 'lib/asciidoctor/html5s/converter.rb', line 571

def url_query(params)
  str = params.map { |k, v|
    next if v.nil? || v.to_s.empty?
    [k, v] * '='
  }.compact.join('&amp;')

  '?' + str unless str.empty?
end

#urlize(*segments) ⇒ Object



309
310
311
312
313
314
315
316
# File 'lib/asciidoctor/html5s/converter.rb', line 309

def urlize(*segments)
  path = segments * '/'
  if path.start_with? '//'
    @_html5s_uri_scheme ||= document.attr('asset-uri-scheme', 'https')
    path = "#{@_html5s_uri_scheme}:#{path}" unless @_html5s_uri_scheme.empty?
  end
  normalize_web_path path
end

#video_iframe?Boolean

Returns true if the video should be embedded in an iframe.

Returns:

  • (Boolean)

    true if the video should be embedded in an iframe.



536
537
538
# File 'lib/asciidoctor/html5s/converter.rb', line 536

def video_iframe?
  ['vimeo', 'youtube'].include? attr(:poster)
end

#video_uriObject



540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
# File 'lib/asciidoctor/html5s/converter.rb', line 540

def video_uri
  case attr(:poster, '').to_sym
  when :vimeo
    params = {
      autoplay: (1 if option? 'autoplay'),
      loop:     (1 if option? 'loop')
    }
    start_anchor = "#at=#{attr :start}" if attr? :start
    "//player.vimeo.com/video/#{attr :target}#{start_anchor}#{url_query params}"

  when :youtube
    video_id, list_id = attr(:target).split('/', 2)
    params = {
      rel:      0,
      start:    (attr :start),
      end:      (attr :end),
      list:     (attr :list, list_id),
      autoplay: (1 if option? 'autoplay'),
      loop:     (1 if option? 'loop'),
      controls: (0 if option? 'nocontrols')
    }
    "//www.youtube.com/embed/#{video_id}#{url_query params}"
  else
    anchor = [attr(:start), attr(:end)].join(',').chomp(',')
    anchor = '' if anchor == ','  # XXX: https://github.com/opal/opal/issues/1902
    anchor = '#t=' + anchor unless anchor.empty?
    media_uri "#{attr :target}#{anchor}"
  end
end

#xref_textString

Returns text of the xref anchor.

Returns:

  • (String)

    text of the xref anchor.



682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
# File 'lib/asciidoctor/html5s/converter.rb', line 682

def xref_text
  str =
    if text
      text
    elsif (path = local_attr :path)
      path
    elsif document.respond_to? :catalog  # Asciidoctor >=1.5.6
      ref = document.catalog[:refs][attr :refid]
      if ref.kind_of? Asciidoctor::AbstractNode
        ref.xreftext((@_html5s_xrefstyle ||= document.attributes['xrefstyle']))
      end
    else  # Asciidoctor < 1.5.6
      document.references[:ids][attr :refid || target]
    end
  (str || "[#{attr :refid}]").tr_s("\n", ' ')
end

#yield_capture(*params) ⇒ Object

Yields the captured block (see #capture).

Parameters:

  • *params

    parameters to pass to the block.

Returns:

  • A content of the captured block.

See Also:



101
102
103
# File 'lib/asciidoctor/html5s/converter.rb', line 101

def yield_capture(*params)
  @_html5s_capture.call(*params) if @_html5s_capture
end