Class: Jekyll::SemTree::DocManager
- Inherits:
-
Object
- Object
- Jekyll::SemTree::DocManager
- Defined in:
- lib/jekyll-semtree/patch/doc_manager.rb
Overview
note: this is a copy/paste from Jekyll-SemTree;
if that plugin is installed, this is all redundant code.
this class is responsible for answering any questions related to jekyll markdown documents that are meant to be processed by the wikirefs plugin.
the following methods are specifically to address two things:
1. ruby's 'find' / 'detect' function does not throw errors if
there are multiple matches. fail fast, i want to know if there
are duplicates.
(not using sets because i don't want to clobber existing documents)
2. handle all jekyll documents in one place. i don't want to
have to filter all documents for target markdown documents
every time i need to check if a file exists.
there is probably a better way to do this…i would prefer to have a plugin-wide function that just wraps all of this and can be called from anywhere in the plugin…but ruby is not a functional language… gotta have classes…
Constant Summary collapse
- CONVERTER_CLASS =
Jekyll::Converters::Markdown
Instance Method Summary collapse
-
#all ⇒ Object
accessors.
-
#file_exists?(filename, file_path = nil) ⇒ Boolean
validators.
- #get_doc_by_fname(filename) ⇒ Object
- #get_doc_by_fpath(file_path) ⇒ Object
- #get_doc_by_url(url) ⇒ Object
- #get_doc_content(filename) ⇒ Object
- #get_image_by_fname(filename) ⇒ Object
-
#initialize(site) ⇒ DocManager
constructor
A new instance of DocManager.
Constructor Details
#initialize(site) ⇒ DocManager
Returns a new instance of DocManager.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/jekyll-semtree/patch/doc_manager.rb', line 31 def initialize(site) return if $wiki_conf.disabled? markdown_converter = site.find_converter_instance(CONVERTER_CLASS) # filter docs based on configs docs = [] docs += site.pages if !$wiki_conf.exclude?(:pages) docs += site.docs_to_write.filter { |d| !$wiki_conf.exclude?(d.type) } @md_docs = docs.filter { |doc| markdown_converter.matches(doc.extname) } if @md_docs.nil? || @md_docs.empty? Jekyll.logger.warn("Jekyll-SemTree: No documents to process.") end @static_files ||= site.static_files end |
Instance Method Details
#all ⇒ Object
accessors
49 50 51 |
# File 'lib/jekyll-semtree/patch/doc_manager.rb', line 49 def all return @md_docs end |
#file_exists?(filename, file_path = nil) ⇒ Boolean
validators
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/jekyll-semtree/patch/doc_manager.rb', line 90 def file_exists?(filename, file_path=nil) Jekyll.logger.error("Jekyll-SemTree: Must provide a 'filename'") if filename.nil? || filename.empty? if file_path.nil? return false if get_doc_by_fname(filename).nil? && get_image_by_fname(filename).nil? return true else return false if get_doc_by_fpath(file_path).nil? return true end end |
#get_doc_by_fname(filename) ⇒ Object
53 54 55 56 57 58 |
# File 'lib/jekyll-semtree/patch/doc_manager.rb', line 53 def get_doc_by_fname(filename) Jekyll.logger.error("Jekyll-SemTree: Must provide a 'filename'") if filename.nil? || filename.empty? docs = @md_docs.select{ |d| File.basename(d.basename, File.extname(d.basename)) == filename } return nil if docs.nil? || docs.empty? || docs.size > 1 return docs[0] end |
#get_doc_by_fpath(file_path) ⇒ Object
60 61 62 63 64 65 |
# File 'lib/jekyll-semtree/patch/doc_manager.rb', line 60 def get_doc_by_fpath(file_path) Jekyll.logger.error("Jekyll-SemTree: Must provide a 'file_path'") if file_path.nil? || file_path.empty? docs = @md_docs.select{ |d| d.relative_path == (file_path + ".md") } return nil if docs.nil? || docs.empty? || docs.size > 1 return docs[0] end |
#get_doc_by_url(url) ⇒ Object
67 68 69 70 71 72 |
# File 'lib/jekyll-semtree/patch/doc_manager.rb', line 67 def get_doc_by_url(url) Jekyll.logger.error("Jekyll-SemTree: Must provide a 'url'") if url.nil? || url.empty? docs = @md_docs.select{ |d| d.url == url } return nil if docs.nil? || docs.empty? || docs.size > 1 return docs[0] end |
#get_doc_content(filename) ⇒ Object
74 75 76 77 78 |
# File 'lib/jekyll-semtree/patch/doc_manager.rb', line 74 def get_doc_content(filename) doc = self.get_doc_by_fname(filename) return nil if docs.nil? return doc.content end |
#get_image_by_fname(filename) ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/jekyll-semtree/patch/doc_manager.rb', line 80 def get_image_by_fname(filename) Jekyll.logger.error("Jekyll-SemTree: Must provide a 'filename'") if filename.nil? || filename.empty? return nil if @static_files.size == 0 || !SUPPORTED_IMG_FORMATS.any?{ |ext| ext == File.extname(filename).downcase } docs = @static_files.select{ |d| File.basename(d.relative_path) == filename } return nil if docs.nil? || docs.empty? || docs.size > 1 return docs[0] end |