Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require "yaml"
- require "fileutils"
- # rake -T
- namespace :doc do
- README = "README.adoc".freeze
- INFO = YAML.load_file("./info.yml")
- MAIN_TITLE = INFO["main_title"]
- SUB_TITLE = INFO["sub_title"]
- LICENSES = INFO["licenses"]
- IMAGE = INFO["image"]
- LINKS = INFO["links"]
- TOC = INFO["toc"]
- desc "HelloWorldを出力"
- task :hello do
- puts "HelloWorld"
- end
- desc "info.ymlからディレクトリ作成とREADME.adocをtouch"
- task :touch do
- if TOC.nil? || TOC["sections"].nil?
- exit 0
- end
- TOC["sections"].each do |section|
- section["chapters"].each.with_index(1) do |chapter, idx|
- dir_name = chapter_dir_name(idx, chapter["title"])
- FileUtils.mkdir_p(dir_name)
- chapter_readme_path = "#{dir_name}/#{README}"
- FileUtils.touch(chapter_readme_path)
- puts "[#{Process.pid}] touched #{chapter_readme_path}"
- end
- end
- puts "DONE"
- end
- desc "info.ymlからTemplateを出力"
- task :template do
- puts summary_template
- end
- # rootに置くREADME.adocをINFOから作成
- def summary_template
- content = ["= #{MAIN_TITLE}"]
- if SUB_TITLE && !SUB_TITLE.empty?
- content << " +"
- content << "~#{SUB_TITLE}~"
- end
- if LICENSES
- content << ""
- LICENSES.each do |license|
- if license["image_url"].nil?
- content << ""
- content << "* #{label}: #{body}"
- elsif license["label"].nil?
- content << "image:#{license["image_url"]}[#{license["body"]}]"
- end
- end
- end
- if IMAGE
- content << ""
- content << "image:#{IMAGE["url"]}[#{IMAGE["msg"]||"img"},width=20%]"
- end
- if LINKS
- content << ""
- LINKS.each do |link|
- content << "* link:#{link["url"]}[#{link["msg"]}]"
- end
- end
- if TOC
- content << ""
- if TOC["chapters"]
- TOC["chapters"].each.with_index(1) do |chapter, idx|
- content << ""
- chapter_dir = chapter_dir(idx, chapter["title"])
- line = "include::#{chapter_dir}/#{README}[]"
- content << line
- end
- end
- end
- content.join("\n")
- end
- # タイトルのスペースは-(ハイフン)でつなぐ
- # @return チャプターのディレクトリ名
- def chapter_dir_name(idx, title)
- "ch#{idx.to_s.rjust(2, "0")}_#{title.to_s.gsub(/[[:space:]]/, "-")}"
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement