Merge branch 'subdirs-support'

This commit is contained in:
kisenka 2015-05-22 21:13:36 +03:00
commit decfc206cf
6 changed files with 94 additions and 14 deletions

View File

@ -0,0 +1,26 @@
require 'rexml/xpath'
require_relative './utils.rb'
module Docs
class SummaryChecker
def self.check(path)
xml = Docs::Utils.md2xml(path)
dir = File.dirname(path)
is_ok = true
missing_files = []
xml.elements.each("//a") do |link|
href = link.attribute('href').to_s
path = "#{dir}/#{href.gsub(/\.html$/, '.md')}"
if !File.file?(path)
is_ok = false unless is_ok
missing_files.push href
break
end
end
missing_files
end
end
end

26
_lib/summary_checker.rb Normal file
View File

@ -0,0 +1,26 @@
require 'rexml/xpath'
require_relative './utils.rb'
module Docs
class SummaryChecker
def self.check(path)
xml = Docs::Utils.md2xml(path)
dir = File.dirname(path)
is_ok = true
missing_files = []
xml.elements.each("//a") do |link|
href = link.attribute('href').to_s
path = "#{dir}/#{href.gsub(/\.html$/, '.md')}"
if !File.file?(path)
is_ok = false unless is_ok
missing_files.push href
break
end
end
missing_files
end
end
end

View File

@ -4,6 +4,7 @@ require 'uri'
require 'kramdown'
require 'rexml/document'
require 'rexml/xpath'
require_relative './utils.rb'
module Docs
class TocGenerator
@ -15,19 +16,7 @@ module Docs
# @return {Hash}
def self.extract(path, kramdown_config = {})
toc = []
content = File.read(path)
kramdown_config = kramdown_config.merge({:html_to_native => true})
kramdown_doc = Kramdown::Document.new(content, kramdown_config)
kramdown_doc_content = <<-XML
<?xml version="1.0" encoding="UTF-8" ?>
<root>
#{kramdown_doc.to_html}
</root>
XML
kramdown_doc_content = kramdown_doc_content.gsub(/<!--(.*?)-->/m, '')
xml = REXML::Document.new kramdown_doc_content
xml = Docs::Utils.md2xml(path)
xml.elements.each("/root/*") do |node|
items = extract_from_node(node)
toc.concat(items) if items != nil

24
_lib/utils.rb Normal file
View File

@ -0,0 +1,24 @@
require 'kramdown'
require 'rexml/document'
module Docs
class Utils
def self.md2xml(path, kramdown_config = {})
content = File.read(path)
kramdown_config = kramdown_config.merge({:html_to_native => true})
kramdown_doc = Kramdown::Document.new(content, kramdown_config)
kramdown_doc_content = <<-XML
<?xml version="1.0" encoding="UTF-8" ?>
<root>
#{kramdown_doc.to_html}
</root>
XML
kramdown_doc_content = kramdown_doc_content.gsub(/<!--(.*?)-->/m, '')
xml = REXML::Document.new kramdown_doc_content
return xml
end
end
end

View File

@ -4,9 +4,24 @@ task :build_toc do
toc_file = ENV['dest'] || "#{src_dir}/HelpTOC.json"
load "#{src_dir}/_lib/toc_generator.rb"
load "#{src_dir}/_lib/summary_checker.rb"
kramdown_config = YAML::load_file("#{src_dir}/_config.yml")['kramdown']
toc = Docs::TocGenerator.extract("#{src_dir}/_SUMMARY.md", kramdown_config)
missing_files = Docs::SummaryChecker.check("#{src_dir}/_SUMMARY.md")
puts "Checking _SUMMARY.md\n"
if missing_files.size > 0
msg = ['Error, following files are missing:']
missing_files.each do |file|
msg.push "- #{file}"
end
raise (msg.join("\n"))
else
puts "_SUMMARY.md is OK"
end
res = File.write("#{toc_file}", toc.to_json)
puts "TOC successfully created in #{toc_file}" if res

@ -1 +1 @@
Subproject commit 701dc19d866c033a1bc00b5f1b36549efec58626
Subproject commit 495895cc963a653d92f119e08bb07006cf8c56e1