Module: Brief::Document::Rendering

Extended by:
ActiveSupport::Concern
Included in:
Brief::Document
Defined in:
lib/brief/document/rendering.rb

Defined Under Namespace

Modules: ClassMethods Classes: HeadingWrapper

Instance Method Summary collapse

Instance Method Details

#script_contents(options = {}) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/brief/document/rendering.rb', line 49

def script_contents(options={})
  <<-EOF
  <script type="text/javascript">
  Brief.documents['#{ self.relative_path }'] = #{ to_model.as_json(options).to_json };
  </script>
  EOF
end

#script_preambleObject



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/brief/document/rendering.rb', line 37

def script_preamble
  <<-EOF
  <script type="text/javascript">
  if(typeof(global)==="undefined"){
    global = window
  }
  global.Brief = global.Brief || {}
  Brief.documents = Brief.documents || {}
  </script>
  EOF
end

#to_html(options = {}) ⇒ Object

Documents can be rendered into HTML.

They will first be put through a Nokogiri processor pipeline which allows us to wrap things in section containers, apply data attributes, and other things to the HTML so that the output HTML retains its relationship to the underlying data and document structure.



63
64
65
66
67
68
69
70
71
72
# File 'lib/brief/document/rendering.rb', line 63

def to_html(options = {})
  html = if options[:wrap] == false
    unwrapped_html
  else
    wrapper = options.fetch(:wrapper, 'div')
    "#{script_preamble if options[:script] && !options[:skip_preamble]}<#{ wrapper } data-brief-model='#{ model_class.type_alias }' data-brief-path='#{ relative_path }'>#{ unwrapped_html }</#{wrapper}>#{ script_contents(options) if options[:script]}"
  end

  html.respond_to?(:html_safe) ? html.html_safe : html.to_s
end

#unwrapped_htmlObject



74
75
76
# File 'lib/brief/document/rendering.rb', line 74

def unwrapped_html
  parser.to_html
end