Created
July 26, 2017 19:50
-
-
Save itkq/f69f8afbf144094451bfe115a4f4485b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'fileutils' | |
require 'nokogiri' | |
require 'open-uri' | |
BASE_URL = 'http://rst-project.com' | |
def ordinal n | |
case n | |
when 1 | |
"1st" | |
when 2 | |
"2nd" | |
when 3 | |
"3rd" | |
else | |
"#{n}th" | |
end | |
end | |
output = [] | |
# Published ep 1 .. 17 (limited) in http://rst-project.com/story/ | |
[*1..17].each do |i| | |
resource = "/story/#{ordinal(i)}_stage.php" | |
url = URI.join(BASE_URL, resource) | |
html = Nokogiri::HTML(open(url)) | |
title = html.css('.news_ttl').text | |
markdown = "# #{i}. #{title}\n" | |
html.css('div.news_txt > p').children.to_a[1..-1].each do |elem| | |
text = case elem.name | |
when "br" | |
"" | |
when "span" | |
if !elem.css('img').empty? | |
img = elem.css('img') | |
img_url = URI.join(BASE_URL, "/story/#{img.attr('src')}") | |
"<img src=\"#{img_url}\" width=\"#{img.attr('width')}\" height=\"#{img.attr('height')}\">\n" | |
elsif !elem.text.empty? | |
"#{elem.text}\n\n" | |
end | |
when "text" | |
elem.text.gsub("\r\n", "").strip + "\n\n" | |
when "ruby" | |
"#{elem.children[0]} (#{elem.children[1].text}) " | |
end | |
next if text.empty? | |
markdown += text | |
end | |
output << markdown | |
end | |
puts output.join("\n") + "\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment