Skip to content

Instantly share code, notes, and snippets.

@ckazu
Created September 25, 2019 09:17
Show Gist options
  • Save ckazu/6df6fc30e9d53c9154f88ce0da1ea328 to your computer and use it in GitHub Desktop.
Save ckazu/6df6fc30e9d53c9154f88ce0da1ea328 to your computer and use it in GitHub Desktop.
#!/usr/env ruby
require 'pp'
require 'json'
require 'fileutils'
FILENAME = 'xxx.json'
DIRNAME = 'converted'
def convert(scrapbox_format)
md = scrapbox_format
# remove backspace
md.gsub!(/\b/) { '' }
# custom links
md.gsub!('[todo.icon]') { 'ToDo' }
md.gsub!('[mtg.icon]') { 'MTG' }
md.gsub!('[_.icon]') { '- [ ]' }
md.gsub!('[x.icon]') { '- [x]' }
md.gsub!('[hr.icon]') { "---" }
# heading
# md.gsub!(/[**** (.*)]/, "# #{$1}")
md.gsub!(/\A\[\*\*\* (.*)\]/) { "## #{$1}" }
md.gsub!(/\A\[\*\* (.*)\]/) { "### #{$1}" }
md.gsub!(/\A\[\* (.*)\]/) { "#### #{$1}" }
# list
md.gsub!(/\A[\s| ]{1}(\S.*)\z/) { "* #{$1}" }
md.gsub!(/\A[\s| ]{2}(\S.*)\z/) { " * #{$1}" }
md.gsub!(/\A[\s| ]{3}(\S.*)\z/) { " * #{$1}" }
md.gsub!(/\A[\s| ]{4}(\S.*)\z/) { " * #{$1}" }
md.gsub!(/\A[\s| ]{5}(\S.*)\z/) { " * #{$1}" }
# code
md.gsub!(/\A>\s(.*)$\z/) { "`#{$1}`" }
md.gsub!(/\A\$\s*(.*)$\z/) { "`$ #{$1}`" }
# remove escape chars
md.delete!("\b")
md.delete!("\e")
md
end
label = "日報"
file = File.open(FILENAME)
pages = JSON.load(file)['pages']
dir = "#{DIRNAME}/#{label}"
FileUtils.mkdir_p(dir)
pages.each do |page|
Dir.chdir(dir) do
if page['title'] =~ Regexp.new("\\A#{label}")
filename = "#{page['title'].gsub('/', '_').delete("\b").delete("\e")}.md"
md = '';
md << %Q|* Created: #{Time.at(page['created']).to_s}\n|
md << %Q|* Updated: #{Time.at(page['updated']).to_s}\n\n|
md << page['lines'][1..].map {|line| convert line }.join("\n\n")
File.open(filename, 'w') {|f| f.puts md }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment