mirror of
https://github.com/seigler/dash-docs
synced 2025-07-27 01:36:13 +00:00
Add New Glossary Page And Developer Search Feature
This commit adds the code necessary to generate a new en/developre-glossary page with entry pages in the en/glossary/ directory, e.g. en/glossary/51-percent-attack. The glossary page and the individual term pages feature a JavaScript search engine (no CGI) for just glossary terms. This search box has also been added to the following pages: * Developer Documentation (the index page) * Developer Guide * Developer Reference * Developer Examples The search box requires the following MIT-licensed libraries: * JQuery * JQuery UI * JQuery UI CSS stylesheet These allow our JS code to be almost trivially simple in js/devsearch.js This commit adds only code. Actual glossary entry data will be added in a subsequent commit.
This commit is contained in:
parent
c2bc8edff3
commit
037cd4553a
17 changed files with 609 additions and 11 deletions
|
@ -35,12 +35,37 @@ require 'yaml'
|
|||
|
||||
## Workaround for inconsistent relative directory
|
||||
path = File.expand_path(File.dirname(__FILE__)) + "/.."
|
||||
## Load terms from file
|
||||
## Load terms from file only if we haven't loaded them before
|
||||
site = context.registers[:site].config
|
||||
if !site.has_key?("crossref")
|
||||
site['crossref'] = YAML.load_file(path + "/_autocrossref.yaml")
|
||||
if !site.has_key?("crossref_loaded")
|
||||
|
||||
## Load refs from file and then downcase them all so we can
|
||||
## easily detect when we define xrefs more than once
|
||||
mixed_case_refs = YAML.load_file(path + "/_autocrossref.yaml")
|
||||
unvalidated_refs = Hash.new
|
||||
mixed_case_refs.each { |key, value|
|
||||
unvalidated_refs[key.to_s.downcase] = value.to_s.downcase
|
||||
}
|
||||
|
||||
if site.has_key?("crossref")
|
||||
## We already have refs loaded, so merge
|
||||
site['crossref'].merge!(unvalidated_refs) {
|
||||
|key, old_value, new_value|
|
||||
|
||||
if old_value != new_value
|
||||
abort("Error: autocrossref key '#{key}' wants to point to both '#{old_value}' and '#{new_value}'")
|
||||
end
|
||||
|
||||
new_value
|
||||
}
|
||||
else
|
||||
## We don't have refs loaded yet, so copy
|
||||
site['crossref'] = unvalidated_refs
|
||||
end
|
||||
site['crossref_loaded'] = true
|
||||
end
|
||||
|
||||
|
||||
## Sort terms by reverse length, so longest matches get linked
|
||||
## first (e.g. "block chain" before "block"). Otherwise short
|
||||
## terms would get linked first and there'd be nothing for long
|
||||
|
@ -76,7 +101,7 @@ require 'yaml'
|
|||
(?!\w) ## Don't match inside words
|
||||
(?!`) ## Don't match strings ending with a tic, unless the xref itself ends with a tic
|
||||
/xmi) {|s|
|
||||
if term[1] == "DO NOT AUTOCROSSREF"
|
||||
if term[1] == "do not autocrossref"
|
||||
s.gsub(/( |$)/, "<!--noref-->\\&")
|
||||
else
|
||||
"[#{s}][#{term[1]}]{:.auto-link}"
|
||||
|
|
131
_plugins/glossary.rb
Normal file
131
_plugins/glossary.rb
Normal file
|
@ -0,0 +1,131 @@
|
|||
# This file is licensed under the MIT License (MIT) available on
|
||||
# http://opensource.org/licenses/MIT.
|
||||
|
||||
require 'yaml'
|
||||
|
||||
module Jekyll
|
||||
|
||||
class GlossaryPage < Page
|
||||
|
||||
def initialize(site, base, lang, srcdir, src, output_directory)
|
||||
@site = site
|
||||
@base = base
|
||||
@dir = '/' + output_directory
|
||||
|
||||
## Output file is the source file base name plus .md for Markdown
|
||||
## (converted later to HTML)
|
||||
output_file = src.split('.')[0] + ".md"
|
||||
|
||||
## Read in the file's YAML header
|
||||
self.read_yaml(File.join(base, srcdir), src)
|
||||
|
||||
## Pass in the full path to enable edit-on-github links
|
||||
self.data["filename"] = srcdir + '/' + src
|
||||
|
||||
## Page Title: <title> - Bitcoin Glossary
|
||||
self.data["title"] = self.data["required"]["title_max_40_characters_no_formatting"] + " - Bitcoin Glossary"
|
||||
|
||||
## Output file is v<version>.md (converted later to HTML)
|
||||
@name = output_file
|
||||
self.process(output_file)
|
||||
|
||||
## Output full path
|
||||
output_full_path = dir + "/" + output_file.gsub('.md', '')
|
||||
|
||||
self.data['layout'] = 'glossary_entry'
|
||||
self.data['category'] = 'glossary_entry'
|
||||
|
||||
## Combine required (displayed) and optional (non-displayed)
|
||||
## synonyms into an array
|
||||
if self.data["optional"]["synonyms_and_pluralizations_not_shown_in_glossary"].nil?
|
||||
mixed_case_terms = self.data["required"]["synonyms_shown_in_glossary_capitalize_first_letter"]
|
||||
else
|
||||
mixed_case_terms = self.data["required"]["synonyms_shown_in_glossary_capitalize_first_letter"] +\
|
||||
self.data["optional"]["synonyms_and_pluralizations_not_shown_in_glossary"]
|
||||
end
|
||||
|
||||
## Downcase all terms so we can easily detect when we create
|
||||
## duplicated autocrossreference links
|
||||
terms = Array.new
|
||||
mixed_case_terms.each { |term|
|
||||
terms.push(term.downcase)
|
||||
}
|
||||
|
||||
## Add all synonyms to the autocrossref hash table for automatic linking
|
||||
site.config["crossref"] = site.config["crossref"] ? site.config["crossref"] : {}
|
||||
for term in terms do
|
||||
site.config["crossref"].merge!({ term => output_full_path }) {
|
||||
|key, old_value, new_value|
|
||||
|
||||
if old_value != new_value
|
||||
abort("Error: autocrossref key '#{key}' wants to point to both '#{old_value}' and '#{new_value}'")
|
||||
end
|
||||
|
||||
new_value
|
||||
}
|
||||
end
|
||||
|
||||
## Create a newline-containing Markdown-formatted string that
|
||||
## includes links for all of our autocrossref-created synonyms.
|
||||
## TODO: this should probably be done as an `include`-able
|
||||
## template for proper division between the logic and
|
||||
## presentation layers
|
||||
site.config["glossary_links"] = site.config["glossary_links"] ? site.config["glossary_links"] : ''
|
||||
site.config["glossary_links"] \
|
||||
+= "[" \
|
||||
+ output_full_path \
|
||||
+ "]: " \
|
||||
+ output_full_path \
|
||||
+ ' "' \
|
||||
+ self.data["required"]["summary_max_255_characters_no_formatting"].chomp() \
|
||||
+ '"' \
|
||||
+ "\n"
|
||||
|
||||
## Add only shown synonyms to the glossary hash-tables-inside-sorted-array
|
||||
## for use in the search box and on the master listing page
|
||||
site.config["devsearches"]["Glossary"] = site.config["devsearches"]["Glossary"] ? site.config["devsearches"]["Glossary"] : []
|
||||
for term in self.data["required"]["synonyms_shown_in_glossary_capitalize_first_letter"] do
|
||||
site.config["devsearches"]["Glossary"].unshift({ term => output_full_path })
|
||||
end
|
||||
|
||||
## Sort the shown synonyms array alphabetically (case
|
||||
## insensitive). We have to do this here because the version of
|
||||
## Jekyll/Liquid we use does not provide the ability to sort
|
||||
## arrays when the template is compiled. Higher version of Jekyll
|
||||
## do support this feature, so if we upgrade to Jekyll 2.2 or
|
||||
## higher, look at doing this at template time to save CPU cycles
|
||||
## and increase flexibility
|
||||
site.config["devsearches"]["Glossary"].sort_by!{|hash|
|
||||
hash.to_s.downcase.gsub(/"=>.*/,'')
|
||||
}
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
class GlossaryPageGenerator < Generator
|
||||
def generate(site)
|
||||
|
||||
#Do nothing if plugin is disabled
|
||||
if !ENV['ENABLED_PLUGINS'].nil? and ENV['ENABLED_PLUGINS'].index('glossary').nil?
|
||||
print 'Glossary disabled' + "\n"
|
||||
return
|
||||
end
|
||||
|
||||
glossary_dir='_data/glossary/en'
|
||||
|
||||
#generate each release based on templates
|
||||
Dir.foreach(glossary_dir) do |file|
|
||||
next if file == '.' or file == '..'
|
||||
lang = 'en'
|
||||
src = file
|
||||
srcdir = '_data/glossary/en'
|
||||
output_directory = lang + '/glossary/'
|
||||
site.pages << GlossaryPage.new(site, site.source, lang, glossary_dir, src, output_directory)
|
||||
end
|
||||
#TODO releases are only generated for English language,
|
||||
#but they could also be translated at some point. They would however
|
||||
#need to fallback to English when no translation is available.
|
||||
end
|
||||
end
|
||||
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue