Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ikaruga777
Created June 7, 2021 21:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ikaruga777/4da797bdf70fa945025a134327e004d7 to your computer and use it in GitHub Desktop.
Save ikaruga777/4da797bdf70fa945025a134327e004d7 to your computer and use it in GitHub Desktop.
TumblrのhtmlをVuePressでもみたい
require 'nokogiri'
require 'erb'
require 'time'
require 'html2md'
require 'pry-byebug'
require 'fileutils'
def make_dir_if_not_exists(path)
FileUtils.mkdir_p(path) unless Dir.exist?(path)
end
def main
targets = Dir.chdir('../posts/html/') { |path| Dir.glob('*.html').map { |f| path + f } }
template = File.open('template.md.erb', &:read)
errors = []
targets.each do |path|
html = File.open(path, &:read)
converter = Converter.new(template)
result = converter.convert(html)
if result.nil?
errors << path
next
end
output_dir = File.join('dist', result[:date].strftime('%Y/%m'))
filename = "#{result[:title].gsub(%r{[/-]|\s}, '_')}.md"
filepath = File.join(output_dir, filename)
make_dir_if_not_exists(output_dir)
File.open(filepath, 'w') do |f|
f.write result[:markdown]
end
end
puts errors
end
# Converter
class Converter
def initialize(template)
@template = template
end
def convert(html)
doc = Nokogiri::HTML.parse(html)
timestamp = doc.css('#timestamp').text
date = Time.parse(timestamp)
title = doc.at('h1')&.text
return nil if title.nil?
body = doc.xpath('//body')
body.at('h1').remove
body = body.inner_html.gsub(/\n/) { '' }.strip
post = {
title: title,
date: date,
body: body
}
b = binding
markdown = ERB.new(@template).result b
{
date: date,
title: title,
markdown: markdown
}
end
end
main
# frozen_string_literal: true
source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
# gem "rails"
gem 'nokogiri'
gem 'html2md'
group :development do
gem 'pry-byebug'
gem 'rubocop'
end
---
title: <%= post[:title] %>
date: <%= post[:date] %>
feed:
enable: true
layout: post
---
<%= post[:body] %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment