Class: Ligarb::AssetManager

Inherits:
Object
  • Object
show all
Defined in:
lib/ligarb/asset_manager.rb

Constant Summary collapse

ASSETS =
{
  highlight: {
    fence_pattern: /language-(?!mermaid|math|functionplot)(\w+)/,
    files: {
      "js/highlight.min.js" => "https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.1/highlight.min.js",
      "css/highlight.css" => "https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.1/styles/github.min.css",
    },
  },
  mermaid: {
    fence_pattern: /class="mermaid"/,
    files: {
      "js/mermaid.min.js" => "https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.min.js",
    },
  },
  katex: {
    fence_pattern: /class="math-(block|inline)"/,
    files: {
      "js/katex.min.js" => "https://cdn.jsdelivr.net/npm/katex@0.16/dist/katex.min.js",
      "css/katex.min.css" => "https://cdn.jsdelivr.net/npm/katex@0.16/dist/katex.min.css",
    },
  },
  functionplot: {
    fence_pattern: /class="functionplot"/,
    files: {
      "js/d3.min.js" => "https://cdn.jsdelivr.net/npm/d3@7/dist/d3.min.js",
      "js/function-plot.min.js" => "https://cdn.jsdelivr.net/npm/function-plot@1/dist/function-plot.js",
    },
  },
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(output_path) ⇒ AssetManager

Returns a new instance of AssetManager.



40
41
42
43
# File 'lib/ligarb/asset_manager.rb', line 40

def initialize(output_path)
  @output_path = output_path
  @needed = Set.new
end

Instance Method Details

#detect(chapters) ⇒ Object

Scan chapter HTML to detect which assets are needed



46
47
48
49
50
51
52
# File 'lib/ligarb/asset_manager.rb', line 46

def detect(chapters)
  combined_html = chapters.map(&:html).join
  ASSETS.each do |name, config|
    @needed << name if combined_html.match?(config[:fence_pattern])
  end
  @needed
end

#need?(name) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/ligarb/asset_manager.rb', line 64

def need?(name)
  @needed.include?(name)
end

#provision!Object

Download assets if not already present



55
56
57
58
59
60
61
62
# File 'lib/ligarb/asset_manager.rb', line 55

def provision!
  @needed.each do |name|
    ASSETS[name][:files].each do |dest_rel, url|
      dest = File.join(@output_path, dest_rel)
      download(url, dest) unless File.exist?(dest)
    end
  end
end