#!/usr/bin/ruby # Place this file in /usr/local/bin/ # It uses Github API to generate the HTML from the Markdown # It includes the Primer CSS stylesheet # It adds the body class needed and some spacing for the body # It opens Chrome with the HTML generated # It deletes the file when you interrupt the process require 'bundler/inline' gemfile do source 'https://rubygems.org' gem 'httparty' end require 'tempfile' require 'json' if ARGV[0] == '-h' puts "Github styled markdown preview.\n\tUsage: `md-preview PATH`.\n\tExample: `md-preview ./README.md`.\nSend a system interrupt CTRL + C to delete the preview file." return end raise 'Missing file path argument. Usage: `md-preview PATH`' unless ARGV[0] text = File.read(ARGV[0]) options = { headers: { 'Accept' => 'application/vnd.github.v3+json', 'User-Agent' => 'HTTParty' }, body: { text: text }.to_json } response = HTTParty.post('https://api.github.com/markdown', options) raise response.body if response.code != 200 body = ['
', response.body, ''].join html = ['', body].join Tempfile.open(['readme', '.html']) do |file| file.write(html) file.rewind `open -a 'Google Chrome' #{file.path}` loop do end end