Skip to content

Commit

Permalink
add rake task download_post
Browse files Browse the repository at this point in the history
  • Loading branch information
johnlinvc committed Dec 24, 2015
1 parent 1418374 commit a7669cc
Showing 1 changed file with 46 additions and 9 deletions.
55 changes: 46 additions & 9 deletions Rakefile
@@ -1,18 +1,55 @@
require "rubygems"
require "pry-byebug"
require 'nokogiri'
require 'uri'

task :parse_file do
require 'nokogiri'
filename = ENV['f']
outputname = ENV['o']
f = File.read(filename)
doc = Nokogiri::Slop(f)
header = <<-HEADER
def header(layout: "page")
<<-HEADER
---
layout: #{layout}
title:
---
HEADER
end

def post_header(time)
<<-HEADER
---
layout: page
layout: post
title:
date: #{time}
comments: true
paginate: true
categories: [Swift blog, Swift, Xcode]
---
HEADER
end

def parse_article(filename)
f = File.read(filename)
doc = Nokogiri::Slop(f)
html = doc.css('article').children.to_html
File.write(outputname,header+html)
html
end

task :parse_file do
filename = ENV['f']
outputname = ENV['o']
parsed_html = parse_article(filename)
File.write(outputname,header + parsed_html)
end

task :download_post do
url = ENV['url']
`aria2c -s 16 -x 16 -j 16 #{url}`
uri = URI(url)
filename = File.basename(uri.path)
origin_post_filename = "_origin_posts/#{filename}.html"
`mv index.html.1 #{origin_post_filename}`
html = parse_article(origin_post_filename)
doc = Nokogiri::HTML(html)
ts = doc.css('time').attribute('datetime').value
t = DateTime.parse(ts)
outputname = "_posts/"+ t.strftime("%Y-%m-%d") + "-#{filename}.html"
File.write(outputname, post_header(t)+html)
end

0 comments on commit a7669cc

Please sign in to comment.