Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

begining with hiearchy in APP (/!\bug on parser)

  • Loading branch information...
commit 21b3f0588aa5dd71b267b0e2a13ef7e699435ca1 1 parent a9288a6
Benoit Larroque authored
Showing with 73 additions and 26 deletions.
  1. +1 −1  Html2Feedbooks.gemspec
  2. +28 −4 lib/app.rb
  3. +44 −21 lib/feedbooks.rb
2  Html2Feedbooks.gemspec
View
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = %q{Html2Feedbooks}
- s.version = '0.2.2'
+ s.version = '0.3'
s.date = %q{2009-04-28}
s.authors = ["Benoit Larroque"]
s.email = "zeta dot ben at gmail dot com"
32 lib/app.rb
View
@@ -3,6 +3,15 @@
require 'net/http'
require 'time'
require 'htmlentities'
+=begin
+def colour(text, colour_code)
+ "#{colour_code}#{text}\e[0m"
+end
+def green(text); colour(text, "\e[32m"); end
+def red(text); colour(text, "\e[31m"); end
+def yellow(text); colour(text, "\e[33m"); end
+def blue(text); colour(text, "\e[34m"); end
+=end
class AtomPost
attr_accessor :title
@@ -12,15 +21,30 @@ class AtomPost
attr_accessor :addr
attr_accessor :user
attr_accessor :pass
+ attr_accessor :type
def initialize(addrs=nil)
self.addr=addrs unless addrs.nil?
end
+ def down_url(entry_url)
+ #STDERR.puts "scanning #{entry_url}"
+ url=URI.parse(entry_url)
+ Net::HTTP.start(url.host,url.port) {|http|
+ req = Net::HTTP::Get.new(url.path)
+ req.basic_auth user,pass unless user.nil?
+ response = http.request(req)
+ doc=Hpricot(response.body)
+ e=doc.at('//entry').at('link[@rel="down"]')
+ return URI.parse(e[:href]).path unless e.nil?
+ }
+ end
+
def send
raise StandardError.new('Missing Address') if addr.nil?
#3: Detailed control
url = URI.parse(addr)
+ #STDERR.puts "sending to #{url}"
req = Net::HTTP::Post.new(url.path)
req.basic_auth user,pass unless user.nil?
@@ -31,18 +55,18 @@ def send
req.body +='<updated>'+date.xmlschema+'</updated>'+"\n"
req.body +='<author><name>'+author+'</name></author>'+"\n"
req.body +='<content>'+recode_text(content)+'</content>'+"\n"
+ req.body +='<category label="'+type+'" term="'+type+'" />'+"\n" unless type.nil?
req.body +='</entry>'+"\n"
req.set_content_type('application/atom+xml;type=entry')
- File.open('/tmp/test4.txt','w') do |f|
- f << req.body
- end
+# STDERR.puts red("Send \n #{req.body.size > 500 ? req.body[0..250]+'[...]'+req.body[-250..-1]: req.body}")
res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
case res
when Net::HTTPSuccess, Net::HTTPRedirection
- # OK
+# STDERR.puts green(res['location']) if res['location']
+ res['location'] if res['location']
else
res.error!
end
65 lib/feedbooks.rb
View
@@ -42,51 +42,74 @@ class Document
def to_feedbooks(conf)
FBSession.new(conf)
#File.open('/tmp/test3.html','w') do |f|
- content.each do |e|
- # f << e.to_feedbooks(conf)
- e.to_feedbooks(conf)
- # f << " \n " * 10
- end
+ content.each do |e|
+ # f << e.to_feedbooks(conf)
+ e.to_feedbooks(conf,nil)
+ # f << " \n " * 10
+ end
#end
end
end
- class Section
- @@level=0
- def to_feedbooks(conf)
- puts "Sending to feedbooks"
+ class FBPost
+ def self.push(conf,tit,cont,type,path=nil)
+ puts "Sending to feedbooks #{tit}"
fb=FBSession.session
- post=AtomPost.new "http://#{fb.host}/#{fb.booktype}/#{fb.bookid}/contents.atom"
- doc=Hpricot('<div xmlns:xhtml="http://www.w3.org/1999/xhtml">'+to_html+'</div>')
- doc.traverse_all_element do |e|
- unless e.is_a?Hpricot::Text
- e.stag.name='xhtml:'+e.stag.name
- e.etag.name='xhtml:'+e.etag.name unless e.etag.nil?
- end
+ if path.nil?
+ post=AtomPost.new "http://#{fb.host}/#{fb.booktype}/#{fb.bookid}/contents.atom"
+ else
+ post=AtomPost.new "http://#{fb.host}#{path}"
end
- post.content=doc.to_html
+
+ post.content=cont
post.user=fb.user
post.pass=fb.pass
post.date=Time.now
post.author=fb.user
- post.title=title
- post.send
+ post.title=tit
+ post.type=type
+ s=post.send
+ post.down_url(s) unless s.nil?
+ end
+ end
+
+ class Section
+ @@level=0
+ @@types=['Part','Chapter','Section']
+ def to_feedbooks(conf,path=nil)
+ fbpath=FBPost.push(conf,title,'',@@types[@@level]||@@types[-1],path)
+ @@level+=1
+ content.each do |e|
+ e.to_feedbooks(conf,fbpath)
+ end
+ @@level-=1
end
alias :old_to_html :to_html
def to_html
ret=nil
- @@level+=1
if @@level==1
ret=old_to_html
else
ret="<h#{@@level+1}>"+title+"</h#{@@level+1}>"+old_to_html
end
- @@level-=1
ret
end
end
+
+ class Text
+ def to_feedbooks(conf,path=nil)
+ doc=Hpricot('<div xmlns:xhtml="http://www.w3.org/1999/xhtml">'+to_html+'</div>')
+ doc.traverse_all_element do |e|
+ unless e.is_a?Hpricot::Text
+ e.stag.name='xhtml:'+e.stag.name
+ e.etag.name='xhtml:'+e.etag.name unless e.etag.nil?
+ end
+ end
+ FBPost.push(conf,'',doc.to_html,"Text",path)
+ end
+ end
end
def ask(txt,disp='Prompt')
Please sign in to comment.
Something went wrong with that request. Please try again.