Class: EBPS::Conversion::Oebps::HtmlFactory

Inherits:
Factory
  • Object
show all
Defined in:
lib/ebps/conversion/oebps.rb

Constant Summary collapse

DEPRECATED_TAGS =
{
  :u    => 'text-decoration: underline;',
  :bold => 'font-weight: bold;'
}

Instance Attribute Summary

Attributes inherited from Factory

#uid

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ HtmlFactory

Returns a new instance of HtmlFactory.



59
60
61
62
63
64
65
66
# File 'lib/ebps/conversion/oebps.rb', line 59

def initialize *args
  super
  @builder.declare! :DOCTYPE, :html, :PUBLIC,
                    "-//W3C//DTD XHTML 1.0 Strict//EN",
                    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
  @uid = "id" << Digest::MD5.hexdigest(@subject.to_s)
  @current_chapter_id = 0
end

Instance Method Details

#bodyObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ebps/conversion/oebps.rb', line 67

def body
  if @link_drug_brand_name
    search_url = ["http://just-medical.oddb.org/de/just-medical/search/zone/drugs/search_query/",
                  @subject.title.delete('®').gsub(/ [0-9,]+%.*/,''),
                  "/search_type/st_sequence#best_result"].join
    @builder.h1 'id' => @uid do |xml|
      xml.a @subject.title, 'href' => search_url
    end
  else
    @builder.h1 @subject.title, 'id' => @uid
  end
  if EBPS.config.html_index \
    && @subject.chapters.any? do |chp| !chp.partial? end
    table_of_contents
  end
  chapters
end

#chaptersObject



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ebps/conversion/oebps.rb', line 84

def chapters
  @subject.chapters.each_with_index do |chapter, idx|
    unless chapter.heading.empty?
      @current_chapter_id = idx.next
      id = "chapter-#{@current_chapter_id}-#@uid"
      @ids.push [id, chapter.heading, []] unless chapter.partial?
      @builder.h3 chapter.heading, 'id' => id
    end
    paragraphs chapter
  end
end

#format(paragraph) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/ebps/conversion/oebps.rb', line 95

def format paragraph
  return unless paragraph.respond_to?(:formats)
  paragraph.formats.each do |format|
    if txt = paragraph.text[format.range]
      reduce_formats format.values, txt
    end
  end
end

#format_cell(cell) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/ebps/conversion/oebps.rb', line 116

def format_cell(cell)
  case cell
  when EBPS::Text::Picture
    format_picture(cell)
  when EBPS::Text::LinkedParagraph
    format_linked_paragraph(cell)
  when EBPS::Text::MultiCell
    cell.contents.map do |content|
      format_cell(content)
    end
  else
    unless cell.is_a? String
      format cell
    end
  end
end

#format_linked_paragraph(paragraph) ⇒ Object



111
112
113
114
115
# File 'lib/ebps/conversion/oebps.rb', line 111

def format_linked_paragraph(paragraph)
  @builder.p 'class' => 'ebps' do |xml|
    xml.a 'href' => paragraph.url do format paragraph end
  end
end

#format_picture(picture) ⇒ Object



103
104
105
106
107
108
109
110
# File 'lib/ebps/conversion/oebps.rb', line 103

def format_picture(picture)
  name = picture.filename
  path = File.join @tmpdir, name
  picture.image.write path
  @builder.p 'class' => 'ebps' do |xml|
    xml.img 'src' => name, 'alt' => ''
  end
end

#format_table(table) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/ebps/conversion/oebps.rb', line 132

def format_table table
  @builder.table 'class' => 'ebps' do |xml|
    xml.tbody do
      table.each_normalized do |row|
        xml.tr do
          row.each do |cell|
            if cell.respond_to?(:col_span)
              xml.td 'colspan' => cell.col_span do format_cell(cell) end
            else
              xml.td do format_cell(cell) end
            end
          end
        end
      end
    end
  end
end

#paragraphs(chapter) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/ebps/conversion/oebps.rb', line 149

def paragraphs chapter
  sections = 0
  chapter.paragraphs.each do |paragraph|
    case paragraph
    when Text::Picture
      format_picture(paragraph)
    when Text::Subheading
      attrs = {}
      _, _, ids = @ids.last
      if ids
        sections += 1
        id = "section-#{@current_chapter_id}.#{sections}-#@uid"
        ids.push [id, paragraph.to_s]
        attrs.store 'id', id
      end
      @builder.h4 attrs do format(paragraph) end
    when Text::Table
      format_table paragraph
    when Text::LinkedParagraph
      format_linked_paragraph(paragraph)
    else
      @builder.p 'class' => 'ebps' do format(paragraph) end
    end
  end
end

#reduce_formats(formats, text) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/ebps/conversion/oebps.rb', line 174

def reduce_formats formats, text
  if formats.empty?
    @builder.send(:_indent)
    @builder.text! text
    @builder << "\n" if @indent
  elsif style = DEPRECATED_TAGS[formats.first.to_sym]
    @builder.span 'style' => style do
      reduce_formats formats[1..-1], text
    end
  else
    @builder.tag! formats.first do
      reduce_formats formats[1..-1], text
    end
  end
end

#table_of_contentsObject



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/ebps/conversion/oebps.rb', line 189

def table_of_contents
  if title = EBPS.config.content_title
    id = "toc-#@uid"
    @ids.push [id, title]
    @builder.h2(title, :id => id)
  end
  @builder.p 'class' => 'ebps' do |xml|
    @subject.chapters.each_with_index do |chapter, idx|
      unless chapter.partial?
        xml.a chapter.heading, 'href' => "#chapter-#{idx.next}-#@uid"
        xml.br
      end
    end
  end
end

#to_htmlObject



204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/ebps/conversion/oebps.rb', line 204

def to_html
  @builder.html "xmlns" => "http://www.w3.org/1999/xhtml" do |xml|
    xml.head do
      xml.meta 'http-equiv' => 'Content-Type',
               'content' => 'text/html;charset=utf-8'
      xml.title @subject.title
      xml.link 'type' => 'text/css', 'rel' => 'stylesheet',
               'href' => File.basename(EBPS.config.stylesheet)
    end
    xml.body do
      body
    end
  end
end