Last active
June 13, 2016 13:56
-
-
Save kou1okada/cd6f7446ded5a6efe6dda9f5e436c056 to your computer and use it in GitHub Desktop.
Markdown to HTML using GitHub API
This file contains hidden or 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
#!/usr/bin/env ruby | |
# | |
# md2html.rb | |
# Copyright (c) 2016 Koichi OKADA. All rights reserved. | |
# This script is distributed under the MIT license. | |
# | |
require 'io/console' | |
require 'json' | |
require 'uri' | |
require 'net/http' | |
require 'optparse' | |
def error(msg) | |
STDERR.puts "\e[31;1mError:\e[30;0m #{msg}" | |
end | |
def warning(msg) | |
STDERR.puts "\e[33;1mWarning:\e[30;0m #{msg}" | |
end | |
def verbose(msg) | |
STDERR.puts msg if $config[:verbose] | |
end | |
def debug(level, msg) | |
STDERR.puts msg if level <= $config[:debug] | |
end | |
http_params = {} | |
json = "" | |
$config = { | |
:debug => 0, | |
} | |
opts = OptParse.new | |
opts.banner = "Usage: #{File.basename $0} [options] file.md ..." | |
opts.on("-u", "--http-username USERNAME", "your github username") {|v| $config[:username] = v} | |
opts.on("-p", "--http-password PASSWORD", "your github password") {|v| $config[:password] = v} | |
opts.on("-v", "--verbose" , "verbose" ) {|v| $config[:verbose] = true} | |
opts.on( "--debug LEVEL", Integer , "set debug level" ) {|v| $config[:debug] = v.to_i} | |
opts.on("-e", "--exec cmd" , "pass result to cmd" ) {|v| $config[:exec] = v} | |
opts.parse! ARGV | |
if ARGV.count <= 0 | |
puts opts.help | |
exit 1 | |
end | |
if ($config[:username] && !$config[:password]) | |
STDERR.print "password: " | |
$config[:password] = STDIN.noecho(&:gets).chop | |
puts | |
p $config[:password] | |
end | |
http_params[:http_basic_authentication] = [ $config[:username], $config[:password] ] if $config[:username] | |
ARGV.each {|src| | |
if src =~ /\.html/ | |
warning "skip '#{src}', because extension is '.html'. " | |
next | |
end | |
dst = "#{File.dirname src}/#{File.basename src, ".*"}.html" | |
verbose "src : #{src}" | |
verbose "dst : #{dst}" | |
text =IO.read(src) | |
if text.nil? | |
warning "#{src}: text is nil." | |
next | |
end | |
uri = URI.parse("https://api.github.com/markdown") | |
verbose "https request: #{uri.host}:#{uri.port}" | |
req = Net::HTTP::Post.new(uri.path) | |
req.basic_auth(*http_params[:http_basic_authentication]) if http_params[:http_basic_authentication] | |
req["Content-Type"] = "application/vnd.github.v3.full+json" | |
req.body = {"text" => text}.to_json | |
http = Net::HTTP.new(uri.host, uri.port) | |
http.use_ssl = true | |
http.set_debug_output STDERR if 5 <= $config[:debug] | |
res = http.start {|http| http.request(req) } | |
case res | |
when Net::HTTPSuccess | |
verbose "http response: success" | |
when Net::HTTPRedirection | |
warning "http response: redirect" | |
next | |
else | |
warning "Net::HTTP failed: #{res.value}" | |
next | |
end | |
debug 1, "response body: #{res.body}" | |
open(dst, "w") {|f| | |
f.write(res.body) | |
} | |
system($config[:exec], dst) if $config[:exec] | |
} |
This file contains hidden or 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
#!/usr/bin/env ruby | |
# | |
# txt2html.rb | |
# Copyright (c) 2016 Koichi OKADA. All rights reserved. | |
# This script is distributed under the MIT license. | |
# | |
require 'optparse' | |
def error(msg) | |
STDERR.puts "\e[31;1mError:\e[30;0m #{msg}" | |
end | |
def warning(msg) | |
STDERR.puts "\e[33;1mWarning:\e[30;0m #{msg}" | |
end | |
def verbose(msg) | |
STDERR.puts msg if $config[:verbose] | |
end | |
def debug(level, msg) | |
STDERR.puts msg if level <= $config[:debug] | |
end | |
$config = { | |
:debug => 0, | |
} | |
opts = OptParse.new | |
opts.banner = "Usage: #{File.basename $0} [options] file.txt ..." | |
opts.on("-v", "--verbose" , "verbose" ) {|v| $config[:verbose] = true} | |
opts.on( "--debug LEVEL", Integer , "set debug level" ) {|v| $config[:debug] = v.to_i} | |
opts.on("-e", "--exec cmd" , "pass result to cmd" ) {|v| $config[:exec] = v} | |
opts.parse! ARGV | |
if ARGV.count <= 0 | |
puts opts.help | |
exit 1 | |
end | |
ARGV.each {|src| | |
if src =~ /\.html/ | |
warning "skip '#{src}', because extension is '.html'. " | |
next | |
end | |
dst = "#{File.dirname src}/#{File.basename src, ".*"}.html" | |
verbose "src : #{src}" | |
verbose "dst : #{dst}" | |
text = IO.read(src) | |
if text.nil? | |
warning "#{src}: text is nil." | |
next | |
end | |
html = "<html><body>" | |
html += text.gsub(/(https?:\/\/[-0-9A-Za-z:\/&%?.+]*)/, '<a href="\1">\1</a>').gsub("\n", "<br>\n") | |
html += "</body></html>" | |
open(dst, "w") {|f| | |
f.write(html) | |
} | |
system($config[:exec], dst) if $config[:exec] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment