Skip to content

Commit

Permalink
Port autotitle helper to Oga
Browse files Browse the repository at this point in the history
  • Loading branch information
onli committed Feb 14, 2020
1 parent 1cf1b64 commit 1dd52fb
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
require 'sinatra/browserid'
require 'thread/pool'
require 'rack/contrib/try_static'
require "open-uri"

module Ursprung
class Ursprung < Sinatra::Application
Expand Down Expand Up @@ -62,25 +63,27 @@ def blogTitle
def autotitle(text)
db = Database.new
Oga.parse_html(text).css("a").map do |link|
if (href = link.attr("href")) && link.attr("title") == nil && href.match(/^https?:/)
if (href = link.attr("href").value) && link.attr("title") == nil && href.match(/^https?:/)
if ((title, _ = db.getCache(href)) == nil)
require 'mechanize' # TODO: Replace to something not using mechanize (-> nokogiri)
agent = Mechanize.new
begin
title = agent.get(href).title
page = URI.parse(URI.escape(href)).read
parsed_page = Oga.parse_html(page)
title = parsed_page.css('title').first.text
rescue Exception => error
warn error.to_s
title = ""
end
db.cache(href, title)
else
old_link = link
link = link.to_s.sub("<a", "<a title=\"#{title}\"")
begin
text[old_link.to_s] = link
rescue IndexError => ie
warn "could not insert #{title}"
end
end

old_link = link.to_xml
link = link.to_xml.sub("<a", "<a title=\"#{title}\"")
begin
text[old_link] = link
rescue IndexError => ie
warn "could not insert #{title}: " + ie.to_s
end

end
end
return text
Expand Down

0 comments on commit 1dd52fb

Please sign in to comment.