# This file is licensed under the MIT License (MIT) available on # http://opensource.org/licenses/MIT. #sitemap.rb generates a sitemap.xml file, which also includes #alternate hreflang for each translated version of each page. require 'yaml' require 'cgi' module Jekyll class SitemapFile < StaticFile def write(dest) # do nothing end end class SitemapGenerator < Generator def generate(site) #Do nothing if plugin is disabled if !ENV['ENABLED_PLUGINS'].nil? and ENV['ENABLED_PLUGINS'].index('sitemap').nil? print 'Sitemap disabled' + "\n" return end #Load translations locs = {} enabled = ENV['ENABLED_LANGS']; enabled = enabled.split(' ') if !enabled.nil? Dir.foreach('_translations') do |file| next if file == '.' or file == '..' or file == 'COPYING' lang=file.split('.')[0] #Ignore lang if disabled if lang != 'en' and !enabled.nil? and !enabled.include?(lang) next end locs[lang] = YAML.load_file('_translations/'+file)[lang] end #Create destination directory if does not exists if !File.directory?(site.dest) Dir.mkdir(site.dest) end File.open(File.join(site.dest, 'sitemap.xml'), 'w+') do |sitemap| #Open sitemap sitemap.puts '' sitemap.puts '' #Add translated pages with their alternative in each languages locs['en']['url'].each do |id,value| locs.each do |lang,value| #Don't add a page if their url is not translated next if locs[lang]['url'][id].nil? or locs[lang]['url'][id] == '' sitemap.puts '' sitemap.puts ' https://dash-docs.github.io/'+lang+'/'+CGI::escape(locs[lang]['url'][id])+'' locs.each do |altlang,value| next if locs[altlang]['url'][id].nil? or locs[altlang]['url'][id] == '' or altlang == lang sitemap.puts ' ' end sitemap.puts '' end end #Add static non-translated pages Dir.glob('en/**/*.{md,html}').concat(Dir.glob('*.{md,html}')).each do |file| next if file == 'index.html' or file == '404.html' or file == 'README.md' #Ignore google webmaster tools data = File.read(file) next if !data.index('google-site-verification:').nil? sitemap.puts '' sitemap.puts ' https://dash-docs.github.io/'+file.gsub('.html','').gsub('.md','')+'' sitemap.puts '' end #Close sitemap sitemap.puts '' end site.static_files << SitemapFile.new(site, site.source, '', 'sitemap.xml') end end end