zetaben-Html2Feedbooks 0.2.2 → 0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. data/lib/app.rb +28 -4
  2. data/lib/feedbooks.rb +44 -21
  3. data/lib/parser.rb +5 -4
  4. metadata +1 -1
data/lib/app.rb CHANGED
@@ -3,6 +3,15 @@ require 'open-uri'
3
3
  require 'net/http'
4
4
  require 'time'
5
5
  require 'htmlentities'
6
+ =begin
7
+ def colour(text, colour_code)
8
+ "#{colour_code}#{text}\e[0m"
9
+ end
10
+ def green(text); colour(text, "\e[32m"); end
11
+ def red(text); colour(text, "\e[31m"); end
12
+ def yellow(text); colour(text, "\e[33m"); end
13
+ def blue(text); colour(text, "\e[34m"); end
14
+ =end
6
15
 
7
16
  class AtomPost
8
17
  attr_accessor :title
@@ -12,15 +21,30 @@ class AtomPost
12
21
  attr_accessor :addr
13
22
  attr_accessor :user
14
23
  attr_accessor :pass
24
+ attr_accessor :type
15
25
 
16
26
  def initialize(addrs=nil)
17
27
  self.addr=addrs unless addrs.nil?
18
28
  end
19
29
 
30
+ def down_url(entry_url)
31
+ #STDERR.puts "scanning #{entry_url}"
32
+ url=URI.parse(entry_url)
33
+ Net::HTTP.start(url.host,url.port) {|http|
34
+ req = Net::HTTP::Get.new(url.path)
35
+ req.basic_auth user,pass unless user.nil?
36
+ response = http.request(req)
37
+ doc=Hpricot(response.body)
38
+ e=doc.at('//entry').at('link[@rel="down"]')
39
+ return URI.parse(e[:href]).path unless e.nil?
40
+ }
41
+ end
42
+
20
43
  def send
21
44
  raise StandardError.new('Missing Address') if addr.nil?
22
45
  #3: Detailed control
23
46
  url = URI.parse(addr)
47
+ #STDERR.puts "sending to #{url}"
24
48
  req = Net::HTTP::Post.new(url.path)
25
49
  req.basic_auth user,pass unless user.nil?
26
50
 
@@ -31,18 +55,18 @@ class AtomPost
31
55
  req.body +='<updated>'+date.xmlschema+'</updated>'+"\n"
32
56
  req.body +='<author><name>'+author+'</name></author>'+"\n"
33
57
  req.body +='<content>'+recode_text(content)+'</content>'+"\n"
58
+ req.body +='<category label="'+type+'" term="'+type+'" />'+"\n" unless type.nil?
34
59
  req.body +='</entry>'+"\n"
35
60
 
36
61
  req.set_content_type('application/atom+xml;type=entry')
37
62
 
38
- File.open('/tmp/test4.txt','w') do |f|
39
- f << req.body
40
- end
63
+ # STDERR.puts red("Send \n #{req.body.size > 500 ? req.body[0..250]+'[...]'+req.body[-250..-1]: req.body}")
41
64
 
42
65
  res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
43
66
  case res
44
67
  when Net::HTTPSuccess, Net::HTTPRedirection
45
- # OK
68
+ # STDERR.puts green(res['location']) if res['location']
69
+ res['location'] if res['location']
46
70
  else
47
71
  res.error!
48
72
  end
data/lib/feedbooks.rb CHANGED
@@ -42,51 +42,74 @@ module HTML2FB
42
42
  def to_feedbooks(conf)
43
43
  FBSession.new(conf)
44
44
  #File.open('/tmp/test3.html','w') do |f|
45
- content.each do |e|
46
- # f << e.to_feedbooks(conf)
47
- e.to_feedbooks(conf)
48
- # f << " \n " * 10
49
- end
45
+ content.each do |e|
46
+ # f << e.to_feedbooks(conf)
47
+ e.to_feedbooks(conf,nil)
48
+ # f << " \n " * 10
49
+ end
50
50
  #end
51
51
  end
52
52
  end
53
53
 
54
- class Section
55
- @@level=0
56
- def to_feedbooks(conf)
57
- puts "Sending to feedbooks"
54
+ class FBPost
55
+ def self.push(conf,tit,cont,type,path=nil)
56
+ puts "Sending to feedbooks #{tit}"
58
57
  fb=FBSession.session
59
- post=AtomPost.new "http://#{fb.host}/#{fb.booktype}/#{fb.bookid}/contents.atom"
60
- doc=Hpricot('<div xmlns:xhtml="http://www.w3.org/1999/xhtml">'+to_html+'</div>')
61
- doc.traverse_all_element do |e|
62
- unless e.is_a?Hpricot::Text
63
- e.stag.name='xhtml:'+e.stag.name
64
- e.etag.name='xhtml:'+e.etag.name unless e.etag.nil?
65
- end
58
+ if path.nil?
59
+ post=AtomPost.new "http://#{fb.host}/#{fb.booktype}/#{fb.bookid}/contents.atom"
60
+ else
61
+ post=AtomPost.new "http://#{fb.host}#{path}"
66
62
  end
67
- post.content=doc.to_html
63
+
64
+ post.content=cont
68
65
  post.user=fb.user
69
66
  post.pass=fb.pass
70
67
  post.date=Time.now
71
68
  post.author=fb.user
72
- post.title=title
73
- post.send
69
+ post.title=tit
70
+ post.type=type
71
+ s=post.send
72
+ post.down_url(s) unless s.nil?
73
+ end
74
+ end
75
+
76
+ class Section
77
+ @@level=0
78
+ @@types=['Part','Chapter','Section']
79
+ def to_feedbooks(conf,path=nil)
80
+ fbpath=FBPost.push(conf,title,'',@@types[@@level]||@@types[-1],path)
81
+ @@level+=1
82
+ content.each do |e|
83
+ e.to_feedbooks(conf,fbpath)
84
+ end
85
+ @@level-=1
74
86
  end
75
87
 
76
88
  alias :old_to_html :to_html
77
89
 
78
90
  def to_html
79
91
  ret=nil
80
- @@level+=1
81
92
  if @@level==1
82
93
  ret=old_to_html
83
94
  else
84
95
  ret="<h#{@@level+1}>"+title+"</h#{@@level+1}>"+old_to_html
85
96
  end
86
- @@level-=1
87
97
  ret
88
98
  end
89
99
  end
100
+
101
+ class Text
102
+ def to_feedbooks(conf,path=nil)
103
+ doc=Hpricot('<div xmlns:xhtml="http://www.w3.org/1999/xhtml">'+to_html+'</div>')
104
+ doc.traverse_all_element do |e|
105
+ unless e.is_a?Hpricot::Text
106
+ e.stag.name='xhtml:'+e.stag.name
107
+ e.etag.name='xhtml:'+e.etag.name unless e.etag.nil?
108
+ end
109
+ end
110
+ FBPost.push(conf,'',doc.to_html,"Text",path)
111
+ end
112
+ end
90
113
  end
91
114
 
92
115
  def ask(txt,disp='Prompt')
data/lib/parser.rb CHANGED
@@ -75,7 +75,7 @@ module HTML2FB
75
75
 
76
76
  tit.each do |a|
77
77
  s=Section.new
78
- tmp=doc.between(a.first.xpath,a.last.nil? ? nil : a.last.xpath).to_html
78
+ tmp=doc.between(a.first.xpath,a.last.nil? ? nil : a.last.xpath).collect{|r| r.to_original_html}.join
79
79
  tmp.sub!(a.first.to_original_html,'')
80
80
  s.content =[Text.new(tmp)]
81
81
  #buggy with entities
@@ -106,11 +106,12 @@ module HTML2FB
106
106
 
107
107
  tit.each do |a|
108
108
  s=Section.new
109
- tmp=doc.between(a.first.xpath,a.last.nil? ? nil : a.last.xpath).to_html
110
- s.content = [Text.new(tmp)]
109
+ tmp=doc.between(a.first.xpath,a.last.nil? ? nil : a.last.xpath).collect{|r| r.to_original_html}
110
+
111
+ s.content = [Text.new(tmp.join)]
111
112
  s.title = extract_text(a.first)
112
113
  el.content.push s
113
- l.content.sub!(tmp,'')
114
+ tmp.each{|t|l.content.sub!(t,'')}
114
115
  l.content.sub!(a.first.to_original_html,'')
115
116
  end
116
117
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zetaben-Html2Feedbooks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: "0.3"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benoit Larroque