File: text-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 (107 lines) | stat: -rw-r--r-- 2,756 bytes parent folder | download | duplicates (5)
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
require 'feedparser/html2text-parser'
require 'feedparser/filesizes'

class String
  # Convert an HTML text to plain text
  def html2text(wrapto = false)
    text = self.clone
    # parse HTML
    p = FeedParser::HTML2TextParser::new(true)
    p.feed(text)
    p.close
    text = p.savedata
    # remove leading and trailing whilespace
    text.gsub!(/\A\s*/m, '')
    text.gsub!(/\s*\Z/m, '')
    # remove whitespace around \n
    text.gsub!(/ *\n/m, "\n")
    text.gsub!(/\n */m, "\n")
    # and duplicates \n
    text.gsub!(/\n\n+/m, "\n\n")
    # and remove duplicated whitespace
    text.gsub!(/[ \t]+/, ' ')

    # finally, wrap the text if requested
    return wrap_text(text, wrapto) if wrapto
    text
  end

  def wrap_text(text, wrapto = 72)
    text.gsub(/(.{1,#{wrapto}})( +|$)\n?/, "\\1\\2\n")
  end
end

module FeedParser
  class Feed
    def to_text(localtime = true, wrapto = false)
      s = ''
      s += "Type: #{@type}\n"
      s += "Encoding: #{@encoding}\n"
      s += "Title: #{@title}\n"
      s += "Link: #{@link}\n"
      if @description
        s += "Description: #{@description.html2text}\n"
      else
        s += "Description:\n"
      end
      s += "Creator: #{@creator}\n"
      s += "\n"
      @items.each do |i|
        s += '*' * 40 + "\n"
        s += i.to_text(localtime, wrapto)
      end
      s
    end
  end

  class FeedItem
    def to_text(localtime = true, wrapto = false, header = true)
      s = ""
      if header
        s += "Item: "
        s += @title if @title
        s += "\n<#{link}>" if link
        if @date
          if localtime
            s += "\nDate: #{@date.to_s}"
          else
            s += "\nDate: #{@date.getutc.to_s}"
          end
        end
        s += "\n"
      else
        s += "<#{link}>\n\n" if link
      end
      s += "#{@content.html2text(wrapto).chomp}\n" if @content
      if @enclosures and @enclosures.length > 0
        s += "\nFiles:"
        @enclosures.each do |e|
          s += "\n #{e[0]} (#{e[1].to_i.to_human_readable}, #{e[2]})"
        end
      end
      if not header
        s += "-- "
      end
      s += "\nFeed: "
      s += @feed.title if @feed.title
      s += "\n<#{@feed.link}>" if @feed.link
      if not header
        s += "\nItem: "
        s += @title if @title
        s += "\n<#{link}>" if link
        if @date
          if localtime
            s += "\nDate: #{@date.to_s}"
          else
            s += "\nDate: #{@date.getutc.to_s}"
          end
        end
      end
      s += "\nAuthor: #{creator}" if creator
      s += "\nSubject: #{@subject}" if @subject
      s += "\nFiled under: #{@categories.join(', ')}" unless @categories.empty?
      s += "\n" # final newline, for compat with history
      s 
    end
  end
end