class PrelandsRails::CreateSimpleSource::Compile::HtmlCompiler

Компилирует html преленда.

Constants

HTML
META

Attributes

body[RW]
compiled[R]
head[RW]

Public Class Methods

new(string, index_css, index_js, lang, static_js_path) click to toggle source

@param [String] string Исходное содержимое входящего html файла. @param [String] index_css Исходное содержимое файла index.css @param [String] index_js Исходное содержимое файла index.js @param [String] lang Локаль

# File lib/prelands_rails/create_simple_source/compile/html_compiler.rb, line 18
def initialize(string, index_css, index_js, lang, static_js_path)
  @string         = string.gsub(/(\r\n?|\n)/, '') # убираем переносы, мешающие регуляркам
  @index_css      = index_css
  @index_js       = index_js
  @lang           = lang
  @static_js_path = static_js_path
  prepare
end

Private Instance Methods

build_body() click to toggle source
# File lib/prelands_rails/create_simple_source/compile/html_compiler.rb, line 72
def build_body
  body_rx = /<body[^>]*>.+<\/body>/
  result  = @string[body_rx]
  @body   = result
  @body  += static_js
end
build_head() click to toggle source

Из входящего head выберем только то, что нужно, добавим meta, впишем авторский стиль из index.css, добавим наш скрипт

# File lib/prelands_rails/create_simple_source/compile/html_compiler.rb, line 45
def build_head
  # фиксируем head
  head_rx = /(?<=<head).+(?=<\/head>)/
  result  = @string[head_rx]

  # фиксируем title
  title_rx = /<title[^>]*>[^<]+<\/title>/
  title    = @string[title_rx]

  # из head выбираем подключаемые скрипты (js)
  head_script_rx = /<script[^>]*>[^<]*<\/script>/
  head_scripts   = result.scan head_script_rx

  # из head выбираем подключаемые линки (css), исключая `index.css`
  head_link_rx = /<link[^>]+>/
  head_links   = result.scan head_link_rx
  head_links   = head_links.select { |string| !string['index.css'] }.compact

  # собираем извлечённое в один массив
  @head = head_scripts.concat(head_links)
  @head.unshift title
  @head.unshift META
  @head << ('<style>%s</style>' % @index_css)
  @head = '<head>%s</head>' % @head.join('')

end
build_html() click to toggle source

собираем результат

# File lib/prelands_rails/create_simple_source/compile/html_compiler.rb, line 80
def build_html
  @compiled =
    HTML % {
      lang: @lang,
      head: @head,
      body: @body
    }
end
prepare() click to toggle source
# File lib/prelands_rails/create_simple_source/compile/html_compiler.rb, line 34
def prepare
  build_head
  build_body
  build_html
end
static_js() click to toggle source
# File lib/prelands_rails/create_simple_source/compile/html_compiler.rb, line 89
        def static_js
          <<-JS
<script>
  (function(d, s, id){
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) {return;}
    js = d.createElement(s); js.id = id;
    js.async = true;
    js.src = "#{@static_js_path}?" + Math.random();
    fjs.parentNode.insertBefore(js, fjs);
  }(document, 'script', 'sdk'));
</script>
          JS
        end