2012-04-15 22:45:39 +02:00
|
|
|
usage 'references'
|
|
|
|
summary 'updates references.mkd'
|
|
|
|
description 'Updates reference file with all internal links.'
|
|
|
|
|
|
|
|
required :s, :sites, 'sites'
|
|
|
|
|
|
|
|
module Nanoc::CLI::Commands
|
|
|
|
class References < ::Nanoc::CLI::CommandRunner
|
|
|
|
|
|
|
|
# collect links in site
|
|
|
|
def extract_links site=nil
|
2012-04-18 23:06:03 +02:00
|
|
|
current_site = load_site site
|
|
|
|
|
2012-05-28 15:50:54 +02:00
|
|
|
page_links = ["<!-- (auto-generated) internal links for #{current_site.shared? ? "shared content" : "site: #{site}"} -->"]
|
|
|
|
|
2012-04-18 23:06:03 +02:00
|
|
|
current_site.printed_items.each do |i|
|
2012-05-28 15:50:54 +02:00
|
|
|
# don't include shared content with sites again
|
|
|
|
unless current_site.shared?
|
2012-04-15 22:45:39 +02:00
|
|
|
next if i.shared?
|
|
|
|
end
|
|
|
|
|
2012-05-01 18:52:32 +02:00
|
|
|
page_links << site_link(i[:title], i.identifier, current_site)
|
2012-04-15 22:45:39 +02:00
|
|
|
|
|
|
|
unless i[:alt_titles].nil?
|
|
|
|
i[:alt_titles].each do |title|
|
2012-05-01 18:52:32 +02:00
|
|
|
page_links << site_link(title, i.identifier, current_site)
|
2012-04-15 22:45:39 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
page_links
|
|
|
|
end
|
2012-04-21 13:55:55 +02:00
|
|
|
|
2012-05-01 18:52:32 +02:00
|
|
|
def site_link title, link, site
|
2012-05-28 15:04:38 +02:00
|
|
|
"[#{title}]: #{site.shared? ? "" : site.url}#{link}"
|
2012-05-01 18:52:32 +02:00
|
|
|
end
|
|
|
|
|
2012-04-21 13:55:55 +02:00
|
|
|
def duplicate_links
|
|
|
|
references = []
|
|
|
|
|
|
|
|
Dir["content/references/*.mkd"].each do |ref|
|
|
|
|
File.open(ref).each_line do |l|
|
2012-07-04 14:58:15 +02:00
|
|
|
if m = l.match(/^ \[ (?<link>.+?) \] : /x)
|
2012-04-21 13:55:55 +02:00
|
|
|
references << {
|
|
|
|
link: m[:link],
|
|
|
|
full_link: l.strip,
|
|
|
|
file: ref,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
dups = false
|
|
|
|
last_ref = nil
|
|
|
|
references.sort_by{|x| x[:link]}.each do |ref|
|
|
|
|
if not last_ref.nil? and ref[:link] == last_ref[:link]
|
|
|
|
puts "Duplicate link '#{ref[:link]}' in '#{ref[:file]}' <-> '#{last_ref[:file]}'!"
|
|
|
|
dups = true
|
|
|
|
end
|
|
|
|
|
|
|
|
last_ref = ref
|
|
|
|
end
|
|
|
|
|
|
|
|
raise "Duplicate links found!" if dups
|
|
|
|
end
|
2012-04-15 22:45:39 +02:00
|
|
|
|
|
|
|
def run
|
|
|
|
([nil] + sites_arg(options[:sites])).each do |site|
|
|
|
|
shared = site.nil?
|
|
|
|
|
|
|
|
# load site
|
|
|
|
if shared
|
|
|
|
puts "loading: shared content"
|
|
|
|
else
|
|
|
|
puts "loading: #{site}"
|
|
|
|
end
|
|
|
|
|
|
|
|
page_links = extract_links site
|
|
|
|
puts "#links: #{page_links.size}"
|
|
|
|
|
|
|
|
# save reference file
|
|
|
|
ref_file = "content/references/site_#{shared ? "shared" : "#{site}"}.mkd"
|
|
|
|
puts "saving to: #{ref_file}"
|
|
|
|
File.open(ref_file, "w").write(page_links.join("\n"))
|
|
|
|
end
|
2012-04-21 13:55:55 +02:00
|
|
|
|
|
|
|
duplicate_links
|
2012-04-15 22:45:39 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
runner Nanoc::CLI::Commands::References
|
|
|
|
|