not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.8 KiB

  1. # Convert Jekyll blog posts to DokuWiki pages
  2. # Adrian Short (https://adrianshort.org/) 15 Feb 2015
  3. require 'fileutils'
  4. require 'yaml'
  5. require 'pp'
  6. require 'pandoc-ruby'
  7. INPUT_DIR = "./_posts"
  8. OUTPUT_BASEDIR = "./blog"
  9. unpublished_files = [] # collect drafts, i.e. files where published == false
  10. # Loop through Markdown files
  11. Dir.glob(File.join(INPUT_DIR, "*.{md,markdown}")) do |fn|
  12. f = File.open(fn)
  13. contents = f.read
  14. metadata = YAML.load(contents)
  15. # skip drafts
  16. if metadata['published'] == false
  17. unpublished_files << metadata['title']
  18. next
  19. end
  20. output = "====== %s ======\n\n" % metadata['title']
  21. output += PandocRuby.convert(contents, :from => :markdown, :to => :dokuwiki)
  22. # convert <!-- more --> tags
  23. output.gsub!(/<HTML>\n<!-- more -->\n<\/HTML>/, '===== =====')
  24. # fix blockquotes (I'm using the DokuWiki blockquote plugin)
  25. output.gsub!(/<HTML><blockquote>\n(.+)<\/blockquote><\/HTML>/m, \
  26. "<blockquote>\n\\1</blockquote>\n")
  27. # Merge categories and tags
  28. tags = metadata['tags'] || []
  29. categories = metadata['categories'] || []
  30. tags = tags.concat(categories).uniq
  31. if tags
  32. # wrap tags containing spaces in double quotes
  33. output += "{{tag>%s}}\n" % tags \
  34. .map{ |t| t.include?(' ') ? "\"%s\"" % t : t } \
  35. .sort_by(&:downcase).join(' ')
  36. end
  37. f.close
  38. # write to new file
  39. out_dir = File.join(OUTPUT_BASEDIR, metadata['date'].year.to_s)
  40. FileUtils.mkdir_p out_dir
  41. out_fn = File.join(out_dir, fn.match(/\d{4}-\d\d-\d\d-(.+)\./)[1] + '.txt')
  42. out_f = File.open(out_fn, 'w') { |f| f << output }
  43. # Set the modified and last access time for the file
  44. # Use `cp -p` to preserve these times when copying
  45. File.utime(metadata['date'], metadata['date'], out_fn)
  46. end
  47. puts "Drafts skipped:"
  48. unpublished_files.each{ |f| puts f }