Skip to content

Instantly share code, notes, and snippets.

@xijo
Last active August 11, 2020 10:20
Show Gist options
  • Save xijo/35adcb27098164b4f45f46ff098bd188 to your computer and use it in GitHub Desktop.
Save xijo/35adcb27098164b4f45f46ff098bd188 to your computer and use it in GitHub Desktop.
Ruby script to convert google docs into markdown
# Usage ruby doc-to-md.rb input.html > output.md
# Make sure to install reverse_markdown: gem install reverse_markdown
require 'reverse_markdown'
require 'cgi'
input = ARGF.read
BOLD_CLASSES = input.scan(/\.(\w+)\s*\{[^}]*font-weight:\s*700[^}]*\}/).flatten
module ReverseMarkdown
module Converters
class Span < Base
def convert(node, state = {})
content = treat_children(node, state.merge(already_strong: true))
classes = node.attributes['class']&.value.to_s.split(' ')
if content.strip.empty? || state[:already_strong]
""
elsif (classes & BOLD_CLASSES).any?
"#{content[/^\s*/]}**#{content.strip}**#{content[/\s*$/]}"
else
content
end
end
def disabled?
!enabled?
end
end
class PreventEmptyLinks < A
def convert(node, state = {})
content = treat_children(node, state)
node['href'] = CGI.unescape(node['href'].gsub!(/https:\/\/www\.google\.com\/url\?q=([^&]+)[^\)]+/, '\1').to_s)
content == "&nbsp;" ? ' ' : " #{super}"
end
end
register :span, Span.new
register :a, PreventEmptyLinks.new
end
end
puts ReverseMarkdown.convert input
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment