Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save wallymathieu/992070 to your computer and use it in GitHub Desktop.
Save wallymathieu/992070 to your computer and use it in GitHub Desktop.
Generate simple index for delicious library
require "rubygems"
require "nokogiri"
def getfiles(dir)
return Dir.new(dir).entries.select {|file| file =~ /[^.]*.html/ }
end
def replaceChars(str)
return str.gsub('å','å').gsub('ä','ä').gsub('ö','ö') \
.gsub('Å','Å').gsub('Ä','Ä').gsub('Ö','ö') \
.gsub(' ','')
end
def getPageAndTitles(dir)
index = {}
getfiles(dir).each{ |file|
doc = Nokogiri::HTML(open(File.join(dir,file)).read)
index[file] = doc.css(".medium").map{|medium|
replaceChars(medium.css("a.creator").inner_html) + " : "+\
replaceChars(medium.css("a.title").inner_html)
}
}
return index.sort{|a,b|
reformat(a)<=>reformat(b)
}
end
def reformat(val)
split = val[0].gsub(/\.html/, '').split('-')
if split[0] == "index":
return "books-00001"
end
if split.length==1:
count = 1
else
count = split[1].to_i
end
return split[0]+ ("%05d" % count)
end
def getPageAndTitlesSimpleHtml(dir,basepath)
return getPageAndTitles(dir).map{ |file,titles|
titlehtml = titles.map{|title| "<li>#{title}</li>" }.join("\n")
"""<div><a href='#{basepath}/#{file}'>#{file}</a>
<ul>
#{titlehtml}
</ul>
</div>
"""
}
end
#
#p doc
File.open("index.html","wb") do |file|
file.puts getPageAndTitlesSimpleHtml("deliciouslibrary",basepath)
end
File.open("iphone.html","wb") do |file|
file.puts getPageAndTitlesSimpleHtml(File.join("deliciouslibrary","iphone"),basepath)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment