Module: Papercraft::HTML

Includes:
Tags
Included in:
HTMLRenderer
Defined in:
lib/papercraft/html.rb

Overview

HTML Markup extensions

Constant Summary collapse

S_HTML5_DOCTYPE =
'<!DOCTYPE html>'

Constants included from Tags

Tags::INITIAL_BUFFER_CAPACITY, Tags::S_EQUAL_QUOTE, Tags::S_GT, Tags::S_GT_LT_SLASH, Tags::S_LT, Tags::S_LT_SLASH, Tags::S_QUOTE, Tags::S_SLASH_GT, Tags::S_SPACE, Tags::S_SPACE_LT_SLASH, Tags::S_TAG_METHOD, Tags::S_TAG_METHOD_LINE, Tags::S_VOID_TAG_METHOD, Tags::S_VOID_TAG_METHOD_LINE

Instance Method Summary collapse

Methods included from Tags

#def_tag, #defer, #extend, #initialize, #method_missing, #orig_extend, #tag, #text, #to_s

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Papercraft::Tags

Instance Method Details

#emit_markdown(markdown, **opts) ⇒ void

This method returns an undefined value.

Converts and emits the given markdown. Papercraft uses [Kramdown](github.com/gettalong/kramdown/) to do the Markdown to HTML conversion. Optional Kramdown settings can be provided in order to control the conversion. Those are merged with the default Kramdown settings, which can be controlled using ‘Papercraft::HTML.kramdown_options`.

Parameters:

  • markdown (String)

    Markdown content

  • **opts (Hash)

    Kramdown options



126
127
128
# File 'lib/papercraft/html.rb', line 126

def emit_markdown(markdown, **opts)
  emit Papercraft.markdown(markdown, **opts)
end

#html5(&block) ⇒ void

This method returns an undefined value.

Emits an HTML5 doctype tag and an html tag with the given block.

Parameters:

  • block (Proc)

    nested HTML block



27
28
29
30
# File 'lib/papercraft/html.rb', line 27

def html5(&block)
  @buffer << S_HTML5_DOCTYPE
  self.html(&block)
end

#import_map(root_path, root_url = '') ⇒ void

This method returns an undefined value.

Emits an import map scrit tag. If a hash is given, emits the hash as is. If a string is given, searches for all *.js files under the given path, and emits an import map including all found files, with versioned URLs.

Parameters:

  • root_path (String, Hash)

    root path or hash

  • root_url (String) (defaults to: '')

    root URL to construct URLs against



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/papercraft/html.rb', line 95

def import_map(root_path, root_url = '')
  if root_path.is_a?(Hash)
    script(root_path.to_json, type: 'importmap')
  else
    map = Dir["#{root_path}/*.js"].sort.each_with_object({}) do |fn, h|
      name = File.basename(fn)
      m = fn.match(/\/([^\/]+)\.js$/)
      h[m[1]] = versioned_file_href(name, root_path, root_url)
    end
    script(map.to_json, type: 'importmap')
  end
end

#js_module(code) ⇒ void

This method returns an undefined value.

Emits a script tag with type attribute set to module.

Parameters:

  • code (String)

    JS code



112
113
114
# File 'lib/papercraft/html.rb', line 112

def js_module(code)
  script code, type: 'module'
end

This method returns an undefined value.

Emits a link element with a stylesheet.

Parameters:

  • href (String)

    stylesheet URL

  • custom_attributes (Hash) (defaults to: nil)

    optional custom attributes for the link element



37
38
39
40
41
42
43
44
45
46
# File 'lib/papercraft/html.rb', line 37

def link_stylesheet(href, custom_attributes = nil)
  attributes = {
    rel: 'stylesheet',
    href: href
  }
  if custom_attributes
    attributes = custom_attributes.merge(attributes)
  end
  link(**attributes)
end

#p(text = nil, **attributes, &block) ⇒ void

This method returns an undefined value.

Emits the p tag (overrides Object#p)

Parameters:

  • text (String) (defaults to: nil)

    text content of tag

  • **attributes (Hash)

    tag attributes



17
18
19
# File 'lib/papercraft/html.rb', line 17

def p(text = nil, **attributes, &block)
  method_missing(:p, text, **attributes, &block)
end

#script(js = nil, **attributes, &block) ⇒ void

This method returns an undefined value.

Emits an inline JS script element.

Parameters:

  • js (String, nil) (defaults to: nil)

    Javascript code

  • **attributes (Hash)

    optional element attributes



65
66
67
68
69
70
71
72
73
74
# File 'lib/papercraft/html.rb', line 65

def script(js = nil, **attributes, &block)
  @buffer << '<script'
  emit_attributes(attributes) unless attributes.empty?

  if js
    @buffer << '>' << js << '</script>'
  else
    @buffer << '></script>'
  end
end

#style(css, **attributes, &block) ⇒ void

This method returns an undefined value.

Emits an inline CSS style element.

Parameters:

  • css (String)

    CSS code

  • **attributes (Hash)

    optional element attributes



53
54
55
56
57
58
# File 'lib/papercraft/html.rb', line 53

def style(css, **attributes, &block)
  @buffer << '<style'
  emit_attributes(attributes) unless attributes.empty?

  @buffer << '>' << css << '</style>'
end

#versioned_file_href(href, root_path, root_url = '') ⇒ String

Returns a versioned URL for the given file spec.

Parameters:

  • href (String)

    relative file path

  • root_path (String)

    root path for file

  • root_url (String) (defaults to: '')

    root URL

Returns:

  • (String)

    versioned URL



82
83
84
85
86
# File 'lib/papercraft/html.rb', line 82

def versioned_file_href(href, root_path, root_url = '')
  fn = File.join(root_path, href)
  version = File.stat(fn).mtime.to_i rescue 0
  "#{root_url}/#{href}?v=#{version}"
end