File: html-output.rb

package info (click to toggle)
ruby-feedparser 0.11.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 228 kB
  • sloc: ruby: 1,871; sh: 24; makefile: 5
file content (180 lines) | stat: -rw-r--r-- 4,321 bytes parent folder | download | duplicates (2)
1
2
3
4
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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
174
175
176
177
178
179
180
require 'feedparser'
require 'feedparser/filesizes'

module FeedParser
  STYLESHEET = <<~EOF
  <style type="text/css">
  body {
    margin: 1em auto;
    padding: 0px 1em;
    max-width: 960px;
  }

  img {
    max-width: 100%;
    height: auto;
  }

  figure {
    margin: 0px;
    max-width: 100%;
    height: auto;
  }

  table.header {
    margin-bottom: 1em;
  }

  table.header, table.metadata, table.attachments {
    font-family: Helvetica, Verdana, sans-serif;
  }

  table.header, table.metadata, table.attachments, pre {
    width: 100%;
    padding: 0.5em;
    background: #eeeeec;
    border: 1px solid #babdb6;
  }

  table.header th, table.metadata th, table.attachments th {
    text-align: right;
    width: 50px;
  }

  blockquote {
    font-style: italic;
    color: #2e3436;
    border-left: 2px solid #babdb6;
    padding-left: 0.5em;
  }

  hr {
    border: none;
    border-top: 1px solid #babdb6;
    margin: 1em auto
  }
  </style>
  EOF
  class Feed
    def to_html(localtime = true)
      s = ''
      s += "<!doctype html>\n"
      s += "<html lang=en>\n"
      s += "<head>\n"
      s += "<meta charset=\"utf-8\"/>\n"
      s += "<title>#{@title.escape_html}</title>\n"
      s += FeedParser::STYLESHEET
      s += "</head>\n"
      s += "<body>\n"

      s += <<-EOF
<table class="feed-header">
      EOF
      r = ""
      r += "<a href=\"#{@link}\">\n" if @link
      if @title
        r += @title.escape_html
      elsif @link
        r += @link.escape_html
      else
        r += "Unnamed feed"
      end
      r += "</a>\n" if @link
      headline = "<tr><th>%s</th>\n<td>%s</td></tr>"
      s += (headline % ["Feed title:", r])
      s += (headline % ["Type:", @type])
      s += (headline % ["Encoding:", @encoding])
      s += (headline % ["Creator:", @creator.escape_html]) if @creator
      s += "</table>\n"

      if @description and @description !~ /\A\s*</m
        s += "<br/>\n"
      end
      s += "#{@description}" if @description

      @items.each do |i|
        s += "\n<hr/><!-- *********************************** -->\n"
        s += i.to_html(localtime)
      end
      s += "\n</body></html>\n"
      s
    end
  end

  class FeedItem
    def to_html_with_headers(localtime = true)
      s = "<!doctype html>\n"
      s += '<html lang="en">'
      s += '<head>'
      s += '<meta charset="utf-8"/>'
      s += "<title>#{@title.escape_html}</title>\n"
      s += FeedParser::STYLESHEET
      s += '</head>'
      s += '<body>'
      s += to_html(localtime)
      s += "</body>"
      s += "</html>"
      s
    end

    def to_html(localtime = true)
      s = <<-EOF
<table class="header">
      EOF
      r = ""
      r += "<a href=\"#{@feed.link}\">\n" if @feed.link
      if @feed.title
        r += @feed.title.escape_html
      elsif @feed.link
        r += @feed.link.escape_html
      else
        r += "Unnamed feed"
      end
      r += "</a>\n" if @feed.link
      headline = "<tr><th>%s</th>\n<td>%s</td></tr>"
      s += (headline % ["Feed:", r])

      r = ""
      r += "<a href=\"#{link}\">" if link
      if @title
        r += @title.escape_html
      elsif link
        r += link.escape_html
      end
      r += "</a>\n" if link
      s += (headline % ["Item:", r])
      s += "</table>\n"
      s += "\n"
      if @content and @content !~ /\A\s*</m
        s += "<br/>\n"
      end
      s += "#{@content}" if @content
      if @enclosures and @enclosures.length > 0
        s += <<-EOF
<table class="attachments">
        EOF
        s += '<tr><th>Files:</th></tr>'
        s += "\n"
        @enclosures.each do |e|
          s += "<tr><td><a href=\"#{e[0]}\">#{e[0].split('/')[-1]}</a> (#{e[1].to_i.to_human_readable}, #{e[2]})</td></tr>\n"
        end
        s += "</table>\n"
      end
      s += "\n<hr/>\n"
      s += '<table class="metadata">' + "\n"
      l = '<tr><th>%s</th><td>%s</td></tr>' + "\n"
      if @date
        if localtime
          s += l % [ 'Date:', @date.to_s ]
        else
          s += l % [ 'Date:', @date.getutc.to_s ]
        end
      end
      s += l % [ 'Author:', creator.escape_html ] if creator
      s += l % [ 'Subject:', @subject.escape_html ] if @subject
      s += l % [ 'Filed under:', @categories.join(', ').escape_html ] unless @categories.empty?
      s += "</table>\n"
      s
    end
  end
end