Skip to content

Commit

Permalink
Merge pull request #202 from kminiatures/add-dash-docset
Browse files Browse the repository at this point in the history
Dash 用の Docset 作成タスクを追加しました。
  • Loading branch information
yasulab committed Aug 17, 2015
2 parents 2ab4e58 + c99c69b commit 1c03db2
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ shots/

## Ignore needless files to host
guides/output/kindle
guides/output/dash

# Ignore for direnv
.envrc
Expand Down
6 changes: 6 additions & 0 deletions guides/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ namespace :guides do
ENV['KINDLE'] = '1'
Rake::Task['guides:generate:html'].invoke
end

desc "Generate .docset File for Dash. https://kapeli.com"
task :dash do
ENV['DASH'] = '1'
Rake::Task['guides:generate:html'].invoke
end
end

# Validate guides -------------------------------------------------------------------------
Expand Down
124 changes: 124 additions & 0 deletions guides/rails_guides/dash.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
require 'redcarpet'
require 'coderay'
require "cgi"

module Dash
extend self

def generate(source_dir, output_dir, out_dir, logfile, debug: false)
@debug = debug
html_body = ''
file_name = 'asset_pipeline'

docset_path = "#{output_dir}/#{out_dir}"
FileUtils.rm_r(docset_path) if Dir.exists? docset_path
@contents_dir = "#{docset_path}/Contents"
@resources_dir = "#{@contents_dir}/Resources"
@documents_dir = "#{@resources_dir}/Documents"

FileUtils.mkdir_p @documents_dir
build_info_plist
initialize_sqlite

Dir.chdir output_dir do
Dir.glob("#{source_dir}/*.md").each do|file_path|
next if file_path =~ /release_notes.md\z/
doc_name = File.basename(file_path).sub(".md", "")

File.open(file_path) do |file|
html_body = create_html_and_register_index(file, doc_name)
end

build_html(doc_name, html_body)
end
end
end

def create_html_and_register_index(file, doc_name)
# CodeBlock
html_body = markdown.render(file.read.gsub("\r\n", "\n").gsub(/^(```)([a-z]{1,10})\n(.*?)\n```/m){|s|
CodeRay.scan($3, $2).div
})
# Create Index <h1>..<h5>
html_body.scan(/<h[1-5]>(.*)<\/h([1-5])>/).each do |attr|
name = attr.first
header = attr.last
plain_name = name.gsub(%r{</?code>}, '').gsub(%r{</?em>}, "")
hash = Digest::MD5.hexdigest name
# Add ID to Header
html_body.sub!("<h#{header}>#{name}</h#{header}>", %{<h#{header} id="#{hash}">#{name}</h#{header}>})
index_name = CGI.unescapeHTML(plain_name).gsub("'"){ "''" }
cmd =%{INSERT OR IGNORE INTO searchIndex(name, type, path) VALUES ('#{index_name}', 'Guide', '#{doc_name}.html##{hash}');}
sqlite cmd
end
html_body
end

def markdown
Redcarpet::Markdown.new(
Redcarpet::Render::HTML,
autolink: true, tables: true,
fenced_code_blocks: true
)
end

def initialize_sqlite
@sqlite_db = "#{@resources_dir}/docSet.dsidx"
sqlite "DROP TABLE IF EXISTS searchIndex;"
sqlite "CREATE TABLE IF NOT EXISTS searchIndex(id INTEGER PRIMARY KEY, name TEXT, type TEXT, path TEXT);"
sqlite "CREATE UNIQUE INDEX anchor ON searchIndex (name, type, path);"
end

def sqlite(query)
puts "[SQLite] #{query}" if @debug
`sqlite3 #{@sqlite_db} "#{query}"`
end

def build_html(file_name, html_body)
File.write("#{@documents_dir}/#{file_name}.html", <<-HTML)
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/main.css">
<style type="text/css">
pre {
background: #002A35!important;
color: #93A1A1!important;
padding: 6px;
border-radius: 2px;
}
</style>
</head>
<body> #{html_body} </body>
</html>
HTML
end

def build_info_plist
file = "#{@contents_dir}/Info.plist"
File.delete if File.exists? file
File.write(file, <<-HTML)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleIdentifier</key>
<string>railsguides</string>
<key>CFBundleName</key>
<string>RailsGuides</string>
<key>DocSetPlatformFamily</key>
<string>railsguides</string>
<key>isDashDocset</key>
<true/>
</dict>
</plist>
HTML
end
end

17 changes: 17 additions & 0 deletions guides/rails_guides/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def set_flags_from_environment
@warnings = ENV['WARNINGS'] == '1'
@all = ENV['ALL'] == '1'
@kindle = ENV['KINDLE'] == '1'
@dash = ENV['DASH'] == '1'
@version = ENV['RAILS_VERSION'] || 'local'
@lang = ENV['GUIDES_LANGUAGE']
end
Expand All @@ -97,6 +98,7 @@ def generate
generate_guides
copy_assets
generate_mobi if kindle?
generate_docset if dash?
end

private
Expand All @@ -105,6 +107,10 @@ def kindle?
@kindle
end

def dash?
@dash
end

def check_for_kindlegen
if `which kindlegen`.blank?
raise "Can't create a kindle version without `kindlegen`."
Expand All @@ -118,6 +124,15 @@ def generate_mobi
puts "(kindlegen log at #{out})."
end

def generate_docset
require 'rails_guides/dash'
out = "#{output_dir}/docset.out"
Dash.generate @source_dir, output_dir,
"ruby_on_rails_guides_#@version%s.docset" % (@lang.present? ? ".#@lang" : ''),
out
puts "(docset generate log at #{out})."
end

def mobi
"ruby_on_rails_guides_#@version%s.mobi" % (@lang.present? ? ".#@lang" : '')
end
Expand All @@ -129,6 +144,8 @@ def initialize_dirs(output)
output
elsif kindle?
"#@guides_dir/output/kindle/#@lang"
elsif dash?
"#@guides_dir/output/dash/#@lang"
else
"#@guides_dir/output/#@lang"
end.sub(%r</$>, '')
Expand Down

0 comments on commit 1c03db2

Please sign in to comment.