Module: Coradoc::Html::AssetResolver

Defined in:
lib/coradoc/html/asset_resolver.rb

Class Method Summary collapse

Class Method Details



56
57
58
59
60
61
62
63
# File 'lib/coradoc/html/asset_resolver.rb', line 56

def css_link_tag(options = {})
  href = stylesheet_path(options)
  doc = Nokogiri::HTML::Document.new
  node = Nokogiri::XML::Node.new('link', doc)
  node['rel'] = 'stylesheet'
  node['href'] = href
  node.to_html
end

.css_style_tag(options = {}) ⇒ Object



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

def css_style_tag(options = {})
  css_content = embedded_stylesheet(options)
  custom_css = options[:custom_css]

  content = css_content
  content += "\n\n#{custom_css}" if custom_css && !custom_css.empty?

  build_text_element('style', content)
end

.css_tags(options = {}) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/coradoc/html/asset_resolver.rb', line 86

def css_tags(options = {})
  tags = []

  if embed_css?(options)
    tags << css_style_tag(options)
  else
    tags << css_link_tag(options)
    tags << custom_css_tag(options[:custom_css]) if options[:custom_css]
  end

  tags.join("\n")
end

.custom_css_tag(custom_css) ⇒ Object



75
76
77
78
79
# File 'lib/coradoc/html/asset_resolver.rb', line 75

def custom_css_tag(custom_css)
  return '' unless custom_css && !custom_css.empty?

  build_text_element('style', custom_css)
end

.embed_css?(options = {}) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
84
# File 'lib/coradoc/html/asset_resolver.rb', line 81

def embed_css?(options = {})
  !options.fetch(:linkcss, Config::DEFAULT_OPTIONS[:linkcss]) ||
    options.fetch(:embedded, Config::DEFAULT_OPTIONS[:embedded])
end

.embed_js?(options = {}) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
102
103
# File 'lib/coradoc/html/asset_resolver.rb', line 99

def embed_js?(options = {})
  !options.fetch(:linkjs, Config::DEFAULT_OPTIONS[:linkjs]) ||
    options.fetch(:embedded, Config::DEFAULT_OPTIONS[:embedded]) ||
    !options.fetch(:linkcss, Config::DEFAULT_OPTIONS[:linkcss])
end

.embedded_javascript(options = {}) ⇒ Object



116
117
118
119
120
121
# File 'lib/coradoc/html/asset_resolver.rb', line 116

def embedded_javascript(options = {})
  javascript_name = options[:javascript] || Config::DEFAULT_OPTIONS[:javascript]
  asset_path = File.join(__dir__, 'assets', 'js', javascript_name)

  File.exist?(asset_path) ? File.read(asset_path) : ''
end

.embedded_stylesheet(options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/coradoc/html/asset_resolver.rb', line 21

def embedded_stylesheet(options = {})
  css_theme = options[:css_theme] || Config::DEFAULT_OPTIONS[:css_theme]
  stylesheet_name = "#{css_theme}.css"

  themes_path = File.join(__dir__, 'assets', 'themes', stylesheet_name)
  asset_path = if File.exist?(themes_path)
                 themes_path
               else
                 File.join(__dir__, 'assets', stylesheet_name)
               end

  css_content = if File.exist?(asset_path)
                  File.read(asset_path)
                else
                  default_path = File.join(__dir__, 'assets', 'coradoc.css')
                  File.exist?(default_path) ? File.read(default_path) : ''
                end

  resolve_css_imports(css_content, File.dirname(asset_path))
end

.highlightjs_tags(options = {}) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/coradoc/html/asset_resolver.rb', line 165

def highlightjs_tags(options = {})
  theme = options[:highlightjs_theme] || Config::DEFAULT_OPTIONS[:highlightjs_theme]
  doc = Nokogiri::HTML::Document.new

  link_node = Nokogiri::XML::Node.new('link', doc)
  link_node['rel'] = 'stylesheet'
  link_node['href'] = "https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/#{theme}.min.css"

  script_node = Nokogiri::XML::Node.new('script', doc)
  script_node['src'] = 'https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js'

  init_node = Nokogiri::XML::Node.new('script', doc)
  init_node.content = 'hljs.highlightAll();'

  [link_node.to_html, script_node.to_html, init_node.to_html].join("\n")
end

.javascript_path(options = {}) ⇒ Object



105
106
107
108
109
110
111
112
113
114
# File 'lib/coradoc/html/asset_resolver.rb', line 105

def javascript_path(options = {})
  javascript = options[:javascript] || Config::DEFAULT_OPTIONS[:javascript]
  jsdir = options[:jsdir] || Config::DEFAULT_OPTIONS[:jsdir]

  if jsdir && jsdir != '.'
    File.join(jsdir, javascript)
  else
    javascript
  end
end


123
124
125
126
127
128
129
130
# File 'lib/coradoc/html/asset_resolver.rb', line 123

def js_link_tag(options = {})
  src = javascript_path(options)
  doc = Nokogiri::HTML::Document.new
  node = Nokogiri::XML::Node.new('script', doc)
  node['src'] = src
  node['defer'] = ''
  node.to_html
end

.js_script_tag(options = {}) ⇒ Object



132
133
134
135
136
137
# File 'lib/coradoc/html/asset_resolver.rb', line 132

def js_script_tag(options = {})
  js_content = embedded_javascript(options)
  return '' if js_content.empty?

  build_text_element('script', js_content)
end

.js_tags(options = {}) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/coradoc/html/asset_resolver.rb', line 139

def js_tags(options = {})
  return '' if options[:javascript] == false

  tags = []

  tags << if embed_js?(options)
            js_script_tag(options)
          else
            js_link_tag(options)
          end

  tags.join("\n")
end

.resolve_css_imports(css_content, base_dir) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/coradoc/html/asset_resolver.rb', line 42

def resolve_css_imports(css_content, base_dir)
  css_content.gsub(/@import\s+(?:url\()?['"]([^'"]+)['"]\)?;?/) do
    import_path = ::Regexp.last_match(1)
    full_path = File.join(base_dir, import_path)

    if File.exist?(full_path)
      imported_content = File.read(full_path)
      resolve_css_imports(imported_content, File.dirname(full_path))
    else
      ::Regexp.last_match(0)
    end
  end
end

.stylesheet_path(options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/coradoc/html/asset_resolver.rb', line 9

def stylesheet_path(options = {})
  css_theme = options[:css_theme] || Config::DEFAULT_OPTIONS[:css_theme]
  stylesheet = "#{css_theme}.css"
  stylesdir = options[:stylesdir] || Config::DEFAULT_OPTIONS[:stylesdir]

  if stylesdir && stylesdir != '.'
    File.join(stylesdir, stylesheet)
  else
    stylesheet
  end
end

.syntax_highlighter_tags(options = {}) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/coradoc/html/asset_resolver.rb', line 153

def syntax_highlighter_tags(options = {})
  highlighter = options[:source_highlighter]
  return '' unless highlighter

  case highlighter.to_sym
  when :highlightjs, :highlight_js, :'highlight.js'
    highlightjs_tags(options)
  else
    ''
  end
end