class Jekyll::ScalaFiddle::ScalaFiddleIntegration

Constants

BODY_END_TAG

Public Class Methods

append_scalafiddle_code(doc) click to toggle source
# File lib/jekyll-scalafiddle.rb, line 121
def append_scalafiddle_code(doc)
  @config = doc.site.config
  if doc.output =~ BODY_END_TAG
    # Insert code before body's end if this document has one.
    location = doc.output.index(BODY_END_TAG)
    doc.output = doc.output.slice(0, location) + api_code(doc) + doc.output.slice(location, doc.output.length - location)
  else
    doc.output.prepend(api_code(doc))
  end
end

Private Class Methods

api_code(page) click to toggle source
# File lib/jekyll-scalafiddle.rb, line 152
        def api_code(page)
          result = ""
          if page.output =~ /<div data-scalafiddle=""/
            templates = page.output.scan(/<div data-scalafiddle="" data-template="([^"]+)"/).flatten
            unless templates.empty?
              result += %Q(
<script>
  window.scalaFiddleTemplates = {
)
              dir = page.site.source + "/" + @config.fetch("scalafiddle", {}).fetch("templateDir", "_scalafiddle")
              js_code = templates.map {|template| load_template(template, dir)}
              result += js_code.map {|template|
                %Q(
    '#{template[:name]}': {
      pre: '#{escape_string(template[:pre])}',
      post: '#{escape_string(template[:post])}'
    }
)
              }.join(",\n")
              result += %Q(
  }
</script>
)
            end
            result += %Q(
<script defer src='#{@config.fetch("scalafiddle", {}).fetch("scalaFiddleUrl", "https://embed.scalafiddle.io/")}integration.js'></script>
)
          end
          result
        end
escape_string(strs) click to toggle source
# File lib/jekyll-scalafiddle.rb, line 147
def escape_string(strs)
  strs.join.gsub(/\\/, "\\\\\\\\").gsub(/\n/, "\\n").gsub(/\r/, "").gsub(/\t/, "\\t").gsub(/'/) {|m| "\\'"}
end
load_template(template, dir) click to toggle source
# File lib/jekyll-scalafiddle.rb, line 133
def load_template(template, dir)
  file = dir + "/" + template + ".scala"
  content = File.readlines(file)
  if content.index {|l| l.start_with?("////")} == nil
    raise RuntimeException, "Template is missing a //// marker"
  end
  {
      :name => template,
      :pre => content.take_while {|l| !l.start_with?("////")},
      :post => content.drop_while {|l| !l.start_with?("////")}.drop(1)
  }
end