Class: NoraMark::Html::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/nora_mark/html/context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(param = {}) ⇒ Context

Returns a new instance of Context.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/nora_mark/html/context.rb', line 5

def initialize(param = {})
  @default_param = param
  @head_inserters = []
  @lang = param[:lang] || 'en'
  @title = param[:title] || 'NoraMark generated document'
  @stylesheets = param[:stylesheets] || []
  @enable_pgroup = param[:enable_pgroup] || true
  self.paragraph_style = param[:paragraph_style]
  @pages = Pages.new(param[:sequence_format])
  @metas = param[:metas] || []
  @namespaces = {}
  @render_parameter = {}
  head_inserter do
    ret = ""
    @stylesheets.each do |s|
      if s.is_a? String
        ret << "<link rel=\"stylesheet\" type=\"text/css\" href=\"#{s}\" />\n"
      elsif s.is_a? Array
        ret << "<link rel=\"stylesheet\" type=\"text/css\" media=\"#{s[1]}\" href=\"#{s[0]}\" />\n"
      else
        raise "Can't use #{s} as a stylesheet"
      end
    end
    ret << @metas.map do |meta|
      "<meta " + meta.map do |k, v|
        "#{k}=\"#{v}\""
      end.join(" ") + " />"
    end.join("\n")
    ret
  end
end

Instance Attribute Details

#enable_pgroupObject

Returns the value of attribute enable_pgroup.



4
5
6
# File 'lib/nora_mark/html/context.rb', line 4

def enable_pgroup
  @enable_pgroup
end

#head_insertersObject

Returns the value of attribute head_inserters.



4
5
6
# File 'lib/nora_mark/html/context.rb', line 4

def head_inserters
  @head_inserters
end

#langObject

Returns the value of attribute lang.



4
5
6
# File 'lib/nora_mark/html/context.rb', line 4

def lang
  @lang
end

#metasObject

Returns the value of attribute metas.



4
5
6
# File 'lib/nora_mark/html/context.rb', line 4

def metas
  @metas
end

#namespacesObject

Returns the value of attribute namespaces.



4
5
6
# File 'lib/nora_mark/html/context.rb', line 4

def namespaces
  @namespaces
end

#render_parameterObject

Returns the value of attribute render_parameter.



4
5
6
# File 'lib/nora_mark/html/context.rb', line 4

def render_parameter
  @render_parameter
end

#stylesheetsObject

Returns the value of attribute stylesheets.



4
5
6
# File 'lib/nora_mark/html/context.rb', line 4

def stylesheets
  @stylesheets
end

#titleObject

Returns the value of attribute title.



4
5
6
# File 'lib/nora_mark/html/context.rb', line 4

def title
  @title
end

Instance Method Details

#<<(text) ⇒ Object



116
117
118
119
120
121
# File 'lib/nora_mark/html/context.rb', line 116

def <<(text)
  if @pages.size == 0 || @pages.last.frozen?
    start_html
  end
  @pages.last << text
end

#chop_last_spaceObject



41
42
43
# File 'lib/nora_mark/html/context.rb', line 41

def chop_last_space
  @pages.last.sub!(/[[:space:]]+\Z/, '')
end

#end_htmlObject



86
87
88
89
90
91
92
93
94
95
# File 'lib/nora_mark/html/context.rb', line 86

def end_html
  return if @pages.size == 0

  page = @pages.last
  if !page.frozen?
    page << "</html>\n"
    page.freeze
  end
  restore_metas
end

#file_basename=(name) ⇒ Object



37
38
39
# File 'lib/nora_mark/html/context.rb', line 37

def file_basename=(name)
  @pages.file_basename = name
end

#head_inserter(&block) ⇒ Object



62
63
64
# File 'lib/nora_mark/html/context.rb', line 62

def head_inserter(&block)
  head_inserters << block
end

#paragraph_styleObject



52
53
54
55
56
57
58
59
60
# File 'lib/nora_mark/html/context.rb', line 52

def paragraph_style
  if @paragraph_style
    @paragraph_style
  elsif @lang.split('-')[0] == 'ja'
    :use_paragraph_group
  else
    :default
  end
end

#paragraph_style=(style) ⇒ Object



45
46
47
48
49
50
# File 'lib/nora_mark/html/context.rb', line 45

def paragraph_style=(style)
  return if style.nil?
  raise "paragrapy_style accepts only :default or :use_paragraph_group but is #{style}" if style != :default && style != :use_paragraph_group

  @paragraph_style = style
end

#restore_metasObject



107
108
109
110
111
112
113
114
# File 'lib/nora_mark/html/context.rb', line 107

def restore_metas
  @stylesheets = @default_param[:stylesheets] || @stylesheets
  @title = @default_param[:title] || @title
  @lang = @default_param[:lang] || @lang
  @paragraph_style = @default_param[:paragraph_style] || @paragraph_style
  @namespaces = @default_param[:namespaces] || @namespaces
  @metas = @default_param[:metas] || @metas
end

#resultObject



127
128
129
130
131
132
133
# File 'lib/nora_mark/html/context.rb', line 127

def result
  if !@pages.last.frozen?
    end_html
  end

  @pages
end

#save_default_metasObject

save metadata written with Frontmatter Writer



98
99
100
101
102
103
104
105
# File 'lib/nora_mark/html/context.rb', line 98

def save_default_metas
  @default_param[:stylesheets] ||= @stylesheets if !@stylesheets.nil? && @stylesheets.size > 0
  @default_param[:title] ||= @title
  @default_param[:lang] ||= @lang
  @default_param[:paragraph_style] ||= @paragraph_style
  @default_param[:namespaces] ||=  @namespaces if !@namespaces.nil? && @namespaces.size > 0
  @default_param[:metas] ||= @metas if !@metas.nil? && @metas.size > 0
end

#set_toc(toc) ⇒ Object



123
124
125
# File 'lib/nora_mark/html/context.rb', line 123

def set_toc toc
  @pages.set_toc toc
end

#start_html(title = nil) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/nora_mark/html/context.rb', line 66

def start_html(title = nil)
  @title = title if !title.nil?
  if @pages.size > 0 && !@pages.last.frozen?
    end_html
  end
  page = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  page << "<html xmlns=\"http://www.w3.org/1999/xhtml\""
  page << @namespaces.map do |k, v|
    " xmlns:#{k}=\"#{v}\""
  end.join(' ')
  page << " lang=\"#{@lang}\" xml:lang=\"#{@lang}\">\n"
  page << "<head>\n"
  page << "<title>#{@title}</title>\n"
  @head_inserters.each { |f|
    page << f.call
  }
  page << "</head>\n"
  @pages << page
end