Module: Coradoc::Html::AssetResolver
- Defined in:
- lib/coradoc/html/asset_resolver.rb
Class Method Summary collapse
- .css_link_tag(options = {}) ⇒ Object
- .css_style_tag(options = {}) ⇒ Object
- .css_tags(options = {}) ⇒ Object
- .custom_css_tag(custom_css) ⇒ Object
- .embed_css?(options = {}) ⇒ Boolean
- .embed_js?(options = {}) ⇒ Boolean
- .embedded_javascript(options = {}) ⇒ Object
- .embedded_stylesheet(options = {}) ⇒ Object
- .highlightjs_tags(options = {}) ⇒ Object
- .javascript_path(options = {}) ⇒ Object
- .js_link_tag(options = {}) ⇒ Object
- .js_script_tag(options = {}) ⇒ Object
- .js_tags(options = {}) ⇒ Object
- .resolve_css_imports(css_content, base_dir) ⇒ Object
- .stylesheet_path(options = {}) ⇒ Object
- .syntax_highlighter_tags(options = {}) ⇒ Object
Class Method Details
.css_link_tag(options = {}) ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/coradoc/html/asset_resolver.rb', line 56 def css_link_tag( = {}) href = stylesheet_path() 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( = {}) css_content = () custom_css = [: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 ( = {}) = [] if () << css_style_tag() else << css_link_tag() << custom_css_tag([:custom_css]) if [:custom_css] end .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
81 82 83 84 |
# File 'lib/coradoc/html/asset_resolver.rb', line 81 def ( = {}) !.fetch(:linkcss, Config::DEFAULT_OPTIONS[:linkcss]) || .fetch(:embedded, Config::DEFAULT_OPTIONS[:embedded]) end |
.embed_js?(options = {}) ⇒ Boolean
99 100 101 102 103 |
# File 'lib/coradoc/html/asset_resolver.rb', line 99 def ( = {}) !.fetch(:linkjs, Config::DEFAULT_OPTIONS[:linkjs]) || .fetch(:embedded, Config::DEFAULT_OPTIONS[:embedded]) || !.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 ( = {}) javascript_name = [: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 ( = {}) css_theme = [: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 ( = {}) theme = [: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( = {}) javascript = [:javascript] || Config::DEFAULT_OPTIONS[:javascript] jsdir = [:jsdir] || Config::DEFAULT_OPTIONS[:jsdir] if jsdir && jsdir != '.' File.join(jsdir, javascript) else javascript end end |
.js_link_tag(options = {}) ⇒ Object
123 124 125 126 127 128 129 130 |
# File 'lib/coradoc/html/asset_resolver.rb', line 123 def js_link_tag( = {}) src = javascript_path() 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( = {}) js_content = () 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 ( = {}) return '' if [:javascript] == false = [] << if () js_script_tag() else js_link_tag() end .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( = {}) css_theme = [:css_theme] || Config::DEFAULT_OPTIONS[:css_theme] stylesheet = "#{css_theme}.css" stylesdir = [: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 ( = {}) highlighter = [:source_highlighter] return '' unless highlighter case highlighter.to_sym when :highlightjs, :highlight_js, :'highlight.js' () else '' end end |