Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. require "yaml"
  2. require "fileutils"
  3.  
  4. # rake -T
  5. namespace :doc do
  6. README = "README.adoc".freeze
  7. INFO = YAML.load_file("./info.yml")
  8. MAIN_TITLE = INFO["main_title"]
  9. SUB_TITLE = INFO["sub_title"]
  10. LICENSES = INFO["licenses"]
  11. IMAGE = INFO["image"]
  12. LINKS = INFO["links"]
  13. TOC = INFO["toc"]
  14.  
  15. desc "HelloWorldを出力"
  16. task :hello do
  17. puts "HelloWorld"
  18. end
  19.  
  20. desc "info.ymlからディレクトリ作成とREADME.adocをtouch"
  21. task :touch do
  22. if TOC.nil? || TOC["sections"].nil?
  23. exit 0
  24. end
  25. TOC["sections"].each do |section|
  26. section["chapters"].each.with_index(1) do |chapter, idx|
  27. dir_name = chapter_dir_name(idx, chapter["title"])
  28. FileUtils.mkdir_p(dir_name)
  29. chapter_readme_path = "#{dir_name}/#{README}"
  30. FileUtils.touch(chapter_readme_path)
  31. puts "[#{Process.pid}] touched #{chapter_readme_path}"
  32. end
  33. end
  34. puts "DONE"
  35. end
  36.  
  37. desc "info.ymlからTemplateを出力"
  38. task :template do
  39. puts summary_template
  40. end
  41.  
  42. # rootに置くREADME.adocをINFOから作成
  43. def summary_template
  44. content = ["= #{MAIN_TITLE}"]
  45. if SUB_TITLE && !SUB_TITLE.empty?
  46. content << " +"
  47. content << "~#{SUB_TITLE}~"
  48. end
  49. if LICENSES
  50. content << ""
  51. LICENSES.each do |license|
  52. if license["image_url"].nil?
  53. content << ""
  54. content << "* #{label}: #{body}"
  55. elsif license["label"].nil?
  56. content << "image:#{license["image_url"]}[#{license["body"]}]"
  57. end
  58. end
  59. end
  60. if IMAGE
  61. content << ""
  62. content << "image:#{IMAGE["url"]}[#{IMAGE["msg"]||"img"},width=20%]"
  63. end
  64. if LINKS
  65. content << ""
  66. LINKS.each do |link|
  67. content << "* link:#{link["url"]}[#{link["msg"]}]"
  68. end
  69. end
  70. if TOC
  71. content << ""
  72. if TOC["chapters"]
  73. TOC["chapters"].each.with_index(1) do |chapter, idx|
  74. content << ""
  75. chapter_dir = chapter_dir(idx, chapter["title"])
  76. line = "include::#{chapter_dir}/#{README}[]"
  77. content << line
  78. end
  79. end
  80. end
  81. content.join("\n")
  82. end
  83.  
  84. # タイトルのスペースは-(ハイフン)でつなぐ
  85. # @return チャプターのディレクトリ名
  86. def chapter_dir_name(idx, title)
  87. "ch#{idx.to_s.rjust(2, "0")}_#{title.to_s.gsub(/[[:space:]]/, "-")}"
  88. end
  89. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement