code
stringlengths
26
124k
docstring
stringlengths
23
125k
func_name
stringlengths
1
98
language
stringclasses
1 value
repo
stringlengths
5
53
path
stringlengths
7
151
url
stringlengths
50
211
license
stringclasses
7 values
def relative_path @relative_path ||= path.sub("#{site.collections_path}/", "") end
The path to the document, relative to the collections_dir. Returns a String path which represents the relative path from the collections_dir to this document.
relative_path
ruby
jekyll/jekyll
lib/jekyll/document.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/document.rb
MIT
def basename_without_ext @basename_without_ext ||= File.basename(path, ".*") end
The base filename of the document, without the file extname. Returns the basename without the file extname.
basename_without_ext
ruby
jekyll/jekyll
lib/jekyll/document.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/document.rb
MIT
def basename @basename ||= File.basename(path) end
The base filename of the document. Returns the base filename of the document.
basename
ruby
jekyll/jekyll
lib/jekyll/document.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/document.rb
MIT
def cleaned_relative_path @cleaned_relative_path ||= relative_path[0..-extname.length - 1] .sub(collection.relative_directory, "") .gsub(%r!\.*\z!, "") end
Produces a "cleaned" relative path. The "cleaned" relative path is the relative path without the extname and with the collection's directory removed as well. This method is useful when building the URL of the document. NOTE: `String#gsub` removes all trailing periods (in comparison to `String#chomp`) Examples: When relative_path is "_methods/site/generate...md": cleaned_relative_path # => "/site/generate" Returns the cleaned relative path of the document.
cleaned_relative_path
ruby
jekyll/jekyll
lib/jekyll/document.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/document.rb
MIT
def asset_file? sass_file? || coffeescript_file? end
Determine whether the document is an asset file. Asset files include CoffeeScript files and Sass/SCSS files. Returns true if the extname belongs to the set of extensions that asset files use.
asset_file?
ruby
jekyll/jekyll
lib/jekyll/document.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/document.rb
MIT
def coffeescript_file? extname == ".coffee" end
Determine whether the document is a CoffeeScript file. Returns true if extname == .coffee, false otherwise.
coffeescript_file?
ruby
jekyll/jekyll
lib/jekyll/document.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/document.rb
MIT
def render_with_liquid? return false if data["render_with_liquid"] == false !(coffeescript_file? || yaml_file? || !Utils.has_liquid_construct?(content)) end
Determine whether the file should be rendered with Liquid. Returns false if the document is either an asset file or a yaml file, or if the document doesn't contain any Liquid Tags or Variables, true otherwise.
render_with_liquid?
ruby
jekyll/jekyll
lib/jekyll/document.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/document.rb
MIT
def no_layout? data["layout"] == "none" end
Determine whether the file should be rendered with a layout. Returns true if the Front Matter specifies that `layout` is set to `none`.
no_layout?
ruby
jekyll/jekyll
lib/jekyll/document.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/document.rb
MIT
def place_in_layout? !(asset_file? || yaml_file? || no_layout?) end
Determine whether the file should be placed into layouts. Returns false if the document is set to `layouts: none`, or is either an asset file or a yaml file. Returns true otherwise.
place_in_layout?
ruby
jekyll/jekyll
lib/jekyll/document.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/document.rb
MIT
def url_placeholders @url_placeholders ||= Drops::UrlDrop.new(self) end
Construct a Hash of key-value pairs which contain a mapping between a key in the URL template and the corresponding value for this document. Returns the Hash of key-value pairs for replacement in the URL.
url_placeholders
ruby
jekyll/jekyll
lib/jekyll/document.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/document.rb
MIT
def permalink data && data.is_a?(Hash) && data["permalink"] end
The permalink for this Document. Permalink is set via the data Hash. Returns the permalink or nil if no permalink was set in the data.
permalink
ruby
jekyll/jekyll
lib/jekyll/document.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/document.rb
MIT
def url @url ||= URL.new( :template => url_template, :placeholders => url_placeholders, :permalink => permalink ).to_s end
The computed URL for the document. See `Jekyll::URL#to_s` for more details. Returns the computed URL for the document.
url
ruby
jekyll/jekyll
lib/jekyll/document.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/document.rb
MIT
def destination(base_directory) @destination ||= {} @destination[base_directory] ||= begin path = site.in_dest_dir(base_directory, URL.unescape_path(url)) if url.end_with? "/" path = File.join(path, "index.html") else path << output_ext unless path.end_with? output_ext end path end end
The full path to the output file. base_directory - the base path of the output directory Returns the full path to the output file of this document.
destination
ruby
jekyll/jekyll
lib/jekyll/document.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/document.rb
MIT
def write(dest) path = destination(dest) FileUtils.mkdir_p(File.dirname(path)) Jekyll.logger.debug "Writing:", path File.write(path, output, :mode => "wb") trigger_hooks(:post_write) end
Write the generated Document file to the destination directory. dest - The String path to the destination dir. Returns nothing.
write
ruby
jekyll/jekyll
lib/jekyll/document.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/document.rb
MIT
def published? !(data.key?("published") && data["published"] == false) end
Whether the file is published or not, as indicated in YAML front-matter Returns 'false' if the 'published' key is specified in the YAML front-matter and is 'false'. Otherwise returns 'true'.
published?
ruby
jekyll/jekyll
lib/jekyll/document.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/document.rb
MIT
def read(opts = {}) Jekyll.logger.debug "Reading:", relative_path if yaml_file? @data = SafeYAML.load_file(path) else begin merge_defaults read_content(**opts) read_post_data rescue StandardError => e handle_read_error(e) end end end
Read in the file and assign the content and data based on the file contents. Merge the frontmatter of the file with the frontmatter default values Returns nothing.
read
ruby
jekyll/jekyll
lib/jekyll/document.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/document.rb
MIT
def to_liquid @to_liquid ||= Drops::DocumentDrop.new(self) end
Create a Liquid-understandable version of this Document. Returns a Hash representing this Document's data.
to_liquid
ruby
jekyll/jekyll
lib/jekyll/document.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/document.rb
MIT
def inspect "#<#{self.class} #{relative_path} collection=#{collection.label}>" end
The inspect string for this document. Includes the relative path and the collection label. Returns the inspect string for this document.
inspect
ruby
jekyll/jekyll
lib/jekyll/document.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/document.rb
MIT
def to_s output || content || "NO CONTENT" end
The string representation for this document. Returns the content of the document
to_s
ruby
jekyll/jekyll
lib/jekyll/document.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/document.rb
MIT
def write? return @write_p if defined?(@write_p) @write_p = collection&.write? && site.publisher.publish?(self) end
Determine whether this document should be written. Based on the Collection to which it belongs. True if the document has a collection and if that collection's #write? method returns true, and if the site's Publisher will publish the document. False otherwise. rubocop:disable Naming/MemoizedInstanceVariableName
write?
ruby
jekyll/jekyll
lib/jekyll/document.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/document.rb
MIT
def excerpt_separator @excerpt_separator ||= (data["excerpt_separator"] || site.config["excerpt_separator"]).to_s end
rubocop:enable Naming/MemoizedInstanceVariableName The Document excerpt_separator, from the YAML Front-Matter or site default excerpt_separator value Returns the document excerpt_separator
excerpt_separator
ruby
jekyll/jekyll
lib/jekyll/document.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/document.rb
MIT
def related_posts @related_posts ||= Jekyll::RelatedPosts.new(self).build end
Calculate related posts. Returns an Array of related Posts.
related_posts
ruby
jekyll/jekyll
lib/jekyll/document.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/document.rb
MIT
def method_missing(method, *args, &blck) if data.key?(method.to_s) Jekyll::Deprecator.deprecation_message "Document##{method} is now a key in the #data hash." Jekyll.logger.warn "", "Called by #{caller(1..1)[0]}." data[method.to_s] else super end end
Override of method_missing to check in @data for the key.
method_missing
ruby
jekyll/jekyll
lib/jekyll/document.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/document.rb
MIT
def categories_from_path(special_dir) if relative_path.start_with?(special_dir) superdirs = [] else superdirs = relative_path.sub(Document.superdirs_regex(special_dir), "") superdirs = superdirs.split(File::SEPARATOR) superdirs.reject! { |c| c.empty? || c == special_dir || c == basename } end merge_data!({ "categories" => superdirs }, :source => "file path") end
Add superdirectories of the special_dir to categories. In the case of es/_posts, 'es' is added as a category. In the case of _posts/es, 'es' is NOT added as a category. Returns nothing.
categories_from_path
ruby
jekyll/jekyll
lib/jekyll/document.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/document.rb
MIT
def symlink?(entry) site.safe && File.symlink?(entry) && symlink_outside_site_source?(entry) end
-- Check if a file is a symlink. NOTE: This can be converted to allowing even in safe, since we use Pathutil#in_path? now. --
symlink?
ruby
jekyll/jekyll
lib/jekyll/entry_filter.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/entry_filter.rb
MIT
def glob_include?(enumerator, entry) entry_with_source = PathManager.join(site.source, entry) entry_is_directory = File.directory?(entry_with_source) enumerator.any? do |pattern| case pattern when String pattern_with_source = PathManager.join(site.source, pattern) File.fnmatch?(pattern_with_source, entry_with_source) || entry_with_source.start_with?(pattern_with_source) || (pattern_with_source == "#{entry_with_source}/" if entry_is_directory) when Regexp pattern.match?(entry_with_source) else false end end end
Check if an entry matches a specific pattern. Returns true if path matches against any glob pattern, else false.
glob_include?
ruby
jekyll/jekyll
lib/jekyll/entry_filter.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/entry_filter.rb
MIT
def initialize(doc) self.doc = doc self.content = extract_excerpt(doc.content) end
Initialize this Excerpt instance. doc - The Document. Returns the new Excerpt.
initialize
ruby
jekyll/jekyll
lib/jekyll/excerpt.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/excerpt.rb
MIT
def data @data ||= doc.data.dup @data.delete("layout") @data.delete("excerpt") @data end
Fetch YAML front-matter data from related doc, without layout key Returns Hash of doc data
data
ruby
jekyll/jekyll
lib/jekyll/excerpt.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/excerpt.rb
MIT
def path File.join(doc.path, "#excerpt") end
'Path' of the excerpt. Returns the path for the doc this excerpt belongs to with #excerpt appended
path
ruby
jekyll/jekyll
lib/jekyll/excerpt.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/excerpt.rb
MIT
def relative_path @relative_path ||= File.join(doc.relative_path, "#excerpt") end
'Relative Path' of the excerpt. Returns the relative_path for the doc this excerpt belongs to with #excerpt appended
relative_path
ruby
jekyll/jekyll
lib/jekyll/excerpt.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/excerpt.rb
MIT
def include?(something) output&.include?(something) || content.include?(something) end
Check if excerpt includes a string Returns true if the string passed in
include?
ruby
jekyll/jekyll
lib/jekyll/excerpt.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/excerpt.rb
MIT
def inspect "<#{self.class} id=#{id}>" end
Returns the shorthand String identifier of this doc.
inspect
ruby
jekyll/jekyll
lib/jekyll/excerpt.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/excerpt.rb
MIT
def sanctify_liquid_tags(head) modified = false tag_names = head.scan(LIQUID_TAG_REGEX) tag_names.flatten! tag_names.reverse_each do |tag_name| next unless liquid_block?(tag_name) next if endtag_regex_stash(tag_name).match?(head) modified = true head << "\n{% end#{tag_name} %}" end print_build_warning if modified head end
append appropriate closing tag(s) (for each Liquid block), to the `head` if the partitioning resulted in leaving the closing tag somewhere in the `tail` partition.
sanctify_liquid_tags
ruby
jekyll/jekyll
lib/jekyll/excerpt.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/excerpt.rb
MIT
def blessed_gems %w( jekyll-compose jekyll-docs jekyll-import ) end
Gems that, if installed, should be loaded. Usually contain subcommands.
blessed_gems
ruby
jekyll/jekyll
lib/jekyll/external.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/external.rb
MIT
def require_if_present(names) Array(names).each do |name| require name rescue LoadError Jekyll.logger.debug "Couldn't load #{name}. Skipping." yield(name, version_constraint(name)) if block_given? false end end
Require a gem or file if it's present, otherwise silently fail. names - a string gem name or array of gem names
require_if_present
ruby
jekyll/jekyll
lib/jekyll/external.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/external.rb
MIT
def version_constraint(gem_name) return "= #{Jekyll::VERSION}" if gem_name.to_s.eql?("jekyll-docs") "> 0" end
The version constraint required to activate a given gem. Usually the gem version requirement is "> 0," because any version will do. In the case of jekyll-docs, however, we require the exact same version as Jekyll. Returns a String version constraint in a parseable form for RubyGems.
version_constraint
ruby
jekyll/jekyll
lib/jekyll/external.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/external.rb
MIT
def require_with_graceful_fail(names) Array(names).each do |name| Jekyll.logger.debug "Requiring:", name.to_s require name rescue LoadError => e Jekyll.logger.error "Dependency Error:", <<~MSG Yikes! It looks like you don't have #{name} or one of its dependencies installed. In order to use Jekyll as currently configured, you'll need to install this gem. If you've run Jekyll with `bundle exec`, ensure that you have included the #{name} gem in your Gemfile as well. The full error message from Ruby is: '#{e.message}' If you run into trouble, you can find helpful resources at https://jekyllrb.com/help/! MSG raise Jekyll::Errors::MissingDependencyException, name end end
Require a gem or gems. If it's not present, show a very nice error message that explains everything and is much more helpful than the normal LoadError. names - a string gem name or array of gem names
require_with_graceful_fail
ruby
jekyll/jekyll
lib/jekyll/external.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/external.rb
MIT
def markdownify(input) @context.registers[:site].find_converter_instance( Jekyll::Converters::Markdown ).convert(input.to_s) end
Convert a Markdown string into HTML output. input - The Markdown String to convert. Returns the HTML formatted String.
markdownify
ruby
jekyll/jekyll
lib/jekyll/filters.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/filters.rb
MIT
def smartify(input) @context.registers[:site].find_converter_instance( Jekyll::Converters::SmartyPants ).convert(input.to_s) end
Convert quotes into smart quotes. input - The String to convert. Returns the smart-quotified String.
smartify
ruby
jekyll/jekyll
lib/jekyll/filters.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/filters.rb
MIT
def sassify(input) @context.registers[:site].find_converter_instance( Jekyll::Converters::Sass ).convert(input) end
Convert a Sass string into CSS output. input - The Sass String to convert. Returns the CSS formatted String.
sassify
ruby
jekyll/jekyll
lib/jekyll/filters.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/filters.rb
MIT
def scssify(input) @context.registers[:site].find_converter_instance( Jekyll::Converters::Scss ).convert(input) end
Convert a Scss string into CSS output. input - The Scss String to convert. Returns the CSS formatted String.
scssify
ruby
jekyll/jekyll
lib/jekyll/filters.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/filters.rb
MIT
def slugify(input, mode = nil) Utils.slugify(input, :mode => mode) end
Slugify a filename or title. input - The filename or title to slugify. mode - how string is slugified Returns the given filename or title as a lowercase URL String. See Utils.slugify for more detail.
slugify
ruby
jekyll/jekyll
lib/jekyll/filters.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/filters.rb
MIT
def xml_escape(input) input.to_s.encode(:xml => :attr).gsub(%r!\A"|"\Z!, "") end
XML escape a string for use. Replaces any special characters with appropriate HTML entity replacements. input - The String to escape. Examples xml_escape('foo "bar" <baz>') # => "foo &quot;bar&quot; &lt;baz&gt;" Returns the escaped String.
xml_escape
ruby
jekyll/jekyll
lib/jekyll/filters.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/filters.rb
MIT
def normalize_whitespace(input) input.to_s.gsub(%r!\s+!, " ").tap(&:strip!) end
Replace any whitespace in the input string with a single space input - The String on which to operate. Returns the formatted String
normalize_whitespace
ruby
jekyll/jekyll
lib/jekyll/filters.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/filters.rb
MIT
def number_of_words(input, mode = nil) cjk_charset = '\p{Han}\p{Katakana}\p{Hiragana}\p{Hangul}' cjk_regex = %r![#{cjk_charset}]!o word_regex = %r![^#{cjk_charset}\s]+!o case mode when "cjk" input.scan(cjk_regex).length + input.scan(word_regex).length when "auto" cjk_count = input.scan(cjk_regex).length cjk_count.zero? ? input.split.length : cjk_count + input.scan(word_regex).length else input.split.length end end
Count the number of words in the input string. input - The String on which to operate. Returns the Integer word count.
number_of_words
ruby
jekyll/jekyll
lib/jekyll/filters.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/filters.rb
MIT
def array_to_sentence_string(array, connector = "and") case array.length when 0 "" when 1 array[0].to_s when 2 "#{array[0]} #{connector} #{array[1]}" else "#{array[0...-1].join(", ")}, #{connector} #{array[-1]}" end end
Join an array of things into a string by separating with commas and the word "and" for the last one. array - The Array of Strings to join. connector - Word used to connect the last 2 items in the array Examples array_to_sentence_string(["apples", "oranges", "grapes"]) # => "apples, oranges, and grapes" Returns the formatted String.
array_to_sentence_string
ruby
jekyll/jekyll
lib/jekyll/filters.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/filters.rb
MIT
def where(input, property, value) return input if !property || value.is_a?(Array) || value.is_a?(Hash) return input unless input.respond_to?(:select) input = input.values if input.is_a?(Hash) input_id = input.hash # implement a hash based on method parameters to cache the end-result # for given parameters. @where_filter_cache ||= {} @where_filter_cache[input_id] ||= {} @where_filter_cache[input_id][property] ||= {} # stash or retrieve results to return @where_filter_cache[input_id][property][value] ||= input.select do |object| compare_property_vs_target(item_property(object, property), value) end.to_a end
Filter an array of objects input - the object array. property - the property within each object to filter by. value - the desired value. Cannot be an instance of Array nor Hash since calling #to_s on them returns their `#inspect` string object. Returns the filtered array of objects
where
ruby
jekyll/jekyll
lib/jekyll/filters.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/filters.rb
MIT
def where_exp(input, variable, expression) return input unless input.respond_to?(:select) input = input.values if input.is_a?(Hash) # FIXME condition = parse_condition(expression) @context.stack do input.select do |object| @context[variable] = object condition.evaluate(@context) end end || [] end
Filters an array of objects against an expression input - the object array variable - the variable to assign each item to in the expression expression - a Liquid comparison expression passed in as a string Returns the filtered array of objects
where_exp
ruby
jekyll/jekyll
lib/jekyll/filters.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/filters.rb
MIT
def find(input, property, value) return input if !property || value.is_a?(Array) || value.is_a?(Hash) return input unless input.respond_to?(:find) input = input.values if input.is_a?(Hash) input_id = input.hash # implement a hash based on method parameters to cache the end-result for given parameters. @find_filter_cache ||= {} @find_filter_cache[input_id] ||= {} @find_filter_cache[input_id][property] ||= {} # stash or retrieve results to return # Since `enum.find` can return nil or false, we use a placeholder string "<__NO MATCH__>" # to validate caching. result = @find_filter_cache[input_id][property][value] ||= input.find do |object| compare_property_vs_target(item_property(object, property), value) end || "<__NO MATCH__>" return nil if result == "<__NO MATCH__>" result end
Search an array of objects and returns the first object that has the queried attribute with the given value or returns nil otherwise. input - the object array. property - the property within each object to search by. value - the desired value. Cannot be an instance of Array nor Hash since calling #to_s on them returns their `#inspect` string object. Returns the found object or nil rubocop:disable Metrics/CyclomaticComplexity
find
ruby
jekyll/jekyll
lib/jekyll/filters.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/filters.rb
MIT
def find_exp(input, variable, expression) return input unless input.respond_to?(:find) input = input.values if input.is_a?(Hash) condition = parse_condition(expression) @context.stack do input.find do |object| @context[variable] = object condition.evaluate(@context) end end end
rubocop:enable Metrics/CyclomaticComplexity Searches an array of objects against an expression and returns the first object for which the expression evaluates to true, or returns nil otherwise. input - the object array variable - the variable to assign each item to in the expression expression - a Liquid comparison expression passed in as a string Returns the found object or nil
find_exp
ruby
jekyll/jekyll
lib/jekyll/filters.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/filters.rb
MIT
def to_integer(input) return 1 if input == true return 0 if input == false input.to_i end
Convert the input into integer input - the object string Returns the integer value
to_integer
ruby
jekyll/jekyll
lib/jekyll/filters.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/filters.rb
MIT
def sort(input, property = nil, nils = "first") raise ArgumentError, "Cannot sort a null object." if input.nil? if property.nil? input.sort else case nils when "first" order = - 1 when "last" order = + 1 else raise ArgumentError, "Invalid nils order: " \ "'#{nils}' is not a valid nils order. It must be 'first' or 'last'." end sort_input(input, property, order) end end
Sort an array of objects input - the object array property - property within each object to filter by nils ('first' | 'last') - nils appear before or after non-nil values Returns the filtered array of objects
sort
ruby
jekyll/jekyll
lib/jekyll/filters.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/filters.rb
MIT
def sort_input(input, property, order) input.map { |item| [item_property(item, property), item] } .sort! do |a_info, b_info| a_property = a_info.first b_property = b_info.first if !a_property.nil? && b_property.nil? - order elsif a_property.nil? && !b_property.nil? + order else a_property <=> b_property || a_property.to_s <=> b_property.to_s end end .map!(&:last) end
Sort the input Enumerable by the given property. If the property doesn't exist, return the sort order respective of which item doesn't have the property. We also utilize the Schwartzian transform to make this more efficient.
sort_input
ruby
jekyll/jekyll
lib/jekyll/filters.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/filters.rb
MIT
def parse_sort_input(property) stringified = property.to_s return property.to_i if INTEGER_LIKE.match?(stringified) return property.to_f if FLOAT_LIKE.match?(stringified) property end
return numeric values as numbers for proper sorting
parse_sort_input
ruby
jekyll/jekyll
lib/jekyll/filters.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/filters.rb
MIT
def parse_condition(exp) parser = Liquid::Parser.new(exp) condition = parse_binary_comparison(parser) parser.consume(:end_of_string) condition end
----------- The following set of code was *adapted* from Liquid::If ----------- ref: https://github.com/Shopify/liquid/blob/ffb0ace30315bbcf3548a0383fab531452060ae8/lib/liquid/tags/if.rb#L84-L107 Parse a string to a Liquid Condition
parse_condition
ruby
jekyll/jekyll
lib/jekyll/filters.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/filters.rb
MIT
def parse_binary_comparison(parser) condition = parse_comparison(parser) first_condition = condition while (binary_operator = parser.id?("and") || parser.id?("or")) child_condition = parse_comparison(parser) condition.send(binary_operator, child_condition) condition = child_condition end first_condition end
Generate a Liquid::Condition object from a Liquid::Parser object additionally processing the parsed expression based on whether the expression consists of binary operations with Liquid operators `and` or `or` - parser: an instance of Liquid::Parser Returns an instance of Liquid::Condition
parse_binary_comparison
ruby
jekyll/jekyll
lib/jekyll/filters.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/filters.rb
MIT
def parse_comparison(parser) left_operand = Liquid::Expression.parse(parser.expression) operator = parser.consume?(:comparison) # No comparison-operator detected. Initialize a Liquid::Condition using only left operand return Liquid::Condition.new(left_operand) unless operator # Parse what remained after extracting the left operand and the `:comparison` operator # and initialize a Liquid::Condition object using the operands and the comparison-operator Liquid::Condition.new(left_operand, operator, Liquid::Expression.parse(parser.expression)) end
Generates a Liquid::Condition object from a Liquid::Parser object based on whether the parsed expression involves a "comparison" operator (e.g. <, ==, >, !=, etc) - parser: an instance of Liquid::Parser Returns an instance of Liquid::Condition
parse_comparison
ruby
jekyll/jekyll
lib/jekyll/filters.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/filters.rb
MIT
def find(path, type, setting) value = nil old_scope = nil matching_sets(path, type).each do |set| if set["values"].key?(setting) && has_precedence?(old_scope, set["scope"]) value = set["values"][setting] old_scope = set["scope"] end end value end
Finds a default value for a given setting, filtered by path and type path - the path (relative to the source) of the page, post or :draft the default is used in type - a symbol indicating whether a :page, a :post or a :draft calls this method Returns the default value or nil if none was found
find
ruby
jekyll/jekyll
lib/jekyll/frontmatter_defaults.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/frontmatter_defaults.rb
MIT
def all(path, type) defaults = {} old_scope = nil matching_sets(path, type).each do |set| if has_precedence?(old_scope, set["scope"]) defaults = Utils.deep_merge_hashes(defaults, set["values"]) old_scope = set["scope"] else defaults = Utils.deep_merge_hashes(set["values"], defaults) end end defaults end
Collects a hash with all default values for a page or post path - the relative path of the page or post type - a symbol indicating the type (:post, :page or :draft) Returns a hash with all default values (an empty hash if there are none)
all
ruby
jekyll/jekyll
lib/jekyll/frontmatter_defaults.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/frontmatter_defaults.rb
MIT
def applies?(scope, path, type) applies_type?(scope, type) && applies_path?(scope, path) end
Checks if a given default setting scope matches the given path and type scope - the hash indicating the scope, as defined in _config.yml path - the path to check for type - the type (:post, :page or :draft) to check for Returns true if the scope applies to the given type and path
applies?
ruby
jekyll/jekyll
lib/jekyll/frontmatter_defaults.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/frontmatter_defaults.rb
MIT
def applies_type?(scope, type) !scope.key?("type") || type&.to_sym.eql?(scope["type"].to_sym) end
Determines whether the scope applies to type. The scope applies to the type if: 1. no 'type' is specified 2. the 'type' in the scope is the same as the type asked about scope - the Hash defaults set being asked about application type - the type of the document being processed / asked about its defaults. Returns true if either of the above conditions are satisfied, otherwise returns false
applies_type?
ruby
jekyll/jekyll
lib/jekyll/frontmatter_defaults.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/frontmatter_defaults.rb
MIT
def valid?(set) set.is_a?(Hash) && set["values"].is_a?(Hash) end
Checks if a given set of default values is valid set - the default value hash, as defined in _config.yml Returns true if the set is valid and can be used in this class
valid?
ruby
jekyll/jekyll
lib/jekyll/frontmatter_defaults.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/frontmatter_defaults.rb
MIT
def has_precedence?(old_scope, new_scope) return true if old_scope.nil? new_path = sanitize_path(new_scope["path"]) old_path = sanitize_path(old_scope["path"]) if new_path.length != old_path.length new_path.length >= old_path.length elsif new_scope.key?("type") true else !old_scope.key? "type" end end
Determines if a new scope has precedence over an old one old_scope - the old scope hash, or nil if there's none new_scope - the new scope hash Returns true if the new scope has precedence over the older rubocop: disable Naming/PredicateName
has_precedence?
ruby
jekyll/jekyll
lib/jekyll/frontmatter_defaults.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/frontmatter_defaults.rb
MIT
def matching_sets(path, type) @matched_set_cache ||= {} @matched_set_cache[path] ||= {} @matched_set_cache[path][type] ||= valid_sets.select do |set| !set.key?("scope") || applies?(set["scope"], path, type) end end
rubocop: enable Naming/PredicateName Collects a list of sets that match the given path and type Returns an array of hashes
matching_sets
ruby
jekyll/jekyll
lib/jekyll/frontmatter_defaults.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/frontmatter_defaults.rb
MIT
def valid_sets sets = @site.config["defaults"] return [] unless sets.is_a?(Array) sets.map do |set| if valid?(set) ensure_time!(update_deprecated_types(set)) else Jekyll.logger.warn "Defaults:", "An invalid front-matter default set was found:" Jekyll.logger.warn set.to_s nil end end.tap(&:compact!) end
Returns a list of valid sets This is not cached to allow plugins to modify the configuration and have their changes take effect Returns an array of hashes
valid_sets
ruby
jekyll/jekyll
lib/jekyll/frontmatter_defaults.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/frontmatter_defaults.rb
MIT
def sanitize_path(path) if path.nil? || path.empty? "" elsif path.start_with?("/") path.gsub(%r!\A/|(?<=[^/])\z!, "") else path end end
Sanitizes the given path by removing a leading slash
sanitize_path
ruby
jekyll/jekyll
lib/jekyll/frontmatter_defaults.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/frontmatter_defaults.rb
MIT
def initialize(site, base, name) @site = site @base = base @name = name if site.theme && site.theme.layouts_path.eql?(base) @base_dir = site.theme.root @path = site.in_theme_dir(base, name) else @base_dir = site.source @path = site.in_source_dir(base, name) end @relative_path = @path.sub(@base_dir, "") self.data = {} process(name) read_yaml(base, name) end
Initialize a new Layout. site - The Site. base - The String path to the source. name - The String filename of the post file.
initialize
ruby
jekyll/jekyll
lib/jekyll/layout.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/layout.rb
MIT
def process(name) self.ext = File.extname(name) end
Extract information from the layout filename. name - The String filename of the layout file. Returns nothing.
process
ruby
jekyll/jekyll
lib/jekyll/layout.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/layout.rb
MIT
def inspect "#<#{self.class} @path=#{@path.inspect}>" end
Returns the object as a debug String.
inspect
ruby
jekyll/jekyll
lib/jekyll/layout.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/layout.rb
MIT
def lookup_variable(context, variable) lookup = context variable.split(".").each do |value| lookup = lookup[value] end lookup || variable end
Lookup a Liquid variable in the given context. context - the Liquid context in question. variable - the variable name, as a string. Returns the value of the variable in the context or the variable name if not found.
lookup_variable
ruby
jekyll/jekyll
lib/jekyll/liquid_extensions.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/liquid_extensions.rb
MIT
def cache @cache ||= {} end
A persistent cache to store and retrieve parsed templates based on the filename via `LiquidRenderer::File#parse` It is emptied when `self.reset` is called.
cache
ruby
jekyll/jekyll
lib/jekyll/liquid_renderer.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/liquid_renderer.rb
MIT
def initialize(writer, level = :info) @messages = [] @writer = writer self.log_level = level end
Public: Create a new instance of a log writer writer - Logger compatible instance log_level - (optional, symbol) the log level Returns nothing
initialize
ruby
jekyll/jekyll
lib/jekyll/log_adapter.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/log_adapter.rb
MIT
def debug(topic, message = nil, &block) write(:debug, topic, message, &block) end
Public: Print a debug message topic - the topic of the message, e.g. "Configuration file", "Deprecation", etc. message - the message detail Returns nothing
debug
ruby
jekyll/jekyll
lib/jekyll/log_adapter.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/log_adapter.rb
MIT
def info(topic, message = nil, &block) write(:info, topic, message, &block) end
Public: Print a message topic - the topic of the message, e.g. "Configuration file", "Deprecation", etc. message - the message detail Returns nothing
info
ruby
jekyll/jekyll
lib/jekyll/log_adapter.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/log_adapter.rb
MIT
def warn(topic, message = nil, &block) write(:warn, topic, message, &block) end
Public: Print a message topic - the topic of the message, e.g. "Configuration file", "Deprecation", etc. message - the message detail Returns nothing
warn
ruby
jekyll/jekyll
lib/jekyll/log_adapter.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/log_adapter.rb
MIT
def error(topic, message = nil, &block) write(:error, topic, message, &block) end
Public: Print an error message topic - the topic of the message, e.g. "Configuration file", "Deprecation", etc. message - the message detail Returns nothing
error
ruby
jekyll/jekyll
lib/jekyll/log_adapter.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/log_adapter.rb
MIT
def message(topic, message = nil) raise ArgumentError, "block or message, not both" if block_given? && message message = yield if block_given? message = message.to_s.gsub(%r!\s+!, " ") topic = formatted_topic(topic, block_given?) out = topic + message messages << out out end
Internal: Build a topic method topic - the topic of the message, e.g. "Configuration file", "Deprecation", etc. message - the message detail Returns the formatted message
message
ruby
jekyll/jekyll
lib/jekyll/log_adapter.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/log_adapter.rb
MIT
def formatted_topic(topic, colon = false) "#{topic}#{colon ? ": " : " "}".rjust(20) end
Internal: Format the topic topic - the topic of the message, e.g. "Configuration file", "Deprecation", etc. colon - Returns the formatted topic statement
formatted_topic
ruby
jekyll/jekyll
lib/jekyll/log_adapter.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/log_adapter.rb
MIT
def write_message?(level_of_message) LOG_LEVELS.fetch(level) <= LOG_LEVELS.fetch(level_of_message) end
Internal: Check if the message should be written given the log level. level_of_message - the Symbol level of message, one of :debug, :info, :warn, :error Returns whether the message should be written.
write_message?
ruby
jekyll/jekyll
lib/jekyll/log_adapter.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/log_adapter.rb
MIT
def write(level_of_message, topic, message = nil, &block) return false unless write_message?(level_of_message) writer.public_send(level_of_message, message(topic, message, &block)) end
Internal: Log a message. level_of_message - the Symbol level of message, one of :debug, :info, :warn, :error topic - the String topic or full message message - the String message (optional) block - a block containing the message (optional) Returns false if the message was not written, otherwise returns the value of calling the appropriate writer method, e.g. writer.info.
write
ruby
jekyll/jekyll
lib/jekyll/log_adapter.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/log_adapter.rb
MIT
def initialize(site, base, dir, name) @site = site @base = base @dir = dir @name = name @path = if site.in_theme_dir(base) == base # we're in a theme site.in_theme_dir(base, dir, name) else site.in_source_dir(base, dir, name) end process(name) read_yaml(PathManager.join(base, dir), name) generate_excerpt if site.config["page_excerpts"] data.default_proc = proc do |_, key| site.frontmatter_defaults.find(relative_path, type, key) end Jekyll::Hooks.trigger :pages, :post_init, self end
Initialize a new Page. site - The Site object. base - The String path to the source. dir - The String path between the source and the file. name - The String filename of the file.
initialize
ruby
jekyll/jekyll
lib/jekyll/page.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/page.rb
MIT
def dir url.end_with?("/") ? url : url_dir end
The generated directory into which the page will be placed upon generation. This is derived from the permalink or, if permalink is absent, will be '/' Returns the String destination directory.
dir
ruby
jekyll/jekyll
lib/jekyll/page.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/page.rb
MIT
def permalink data.nil? ? nil : data["permalink"] end
The full path and filename of the post. Defined in the YAML of the post body. Returns the String permalink or nil if none has been set.
permalink
ruby
jekyll/jekyll
lib/jekyll/page.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/page.rb
MIT
def template if !html? "/:path/:basename:output_ext" elsif index? "/:path/" else Utils.add_permalink_suffix("/:path/:basename", site.permalink_style) end end
The template of the permalink. Returns the template String.
template
ruby
jekyll/jekyll
lib/jekyll/page.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/page.rb
MIT
def url @url ||= URL.new( :template => template, :placeholders => url_placeholders, :permalink => permalink ).to_s end
The generated relative url of this page. e.g. /about.html. Returns the String url.
url
ruby
jekyll/jekyll
lib/jekyll/page.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/page.rb
MIT
def url_placeholders { :path => @dir, :basename => basename, :output_ext => output_ext, } end
Returns a hash of URL placeholder names (as symbols) mapping to the desired placeholder replacements. For details see "url.rb"
url_placeholders
ruby
jekyll/jekyll
lib/jekyll/page.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/page.rb
MIT
def process(name) return unless name self.ext = File.extname(name) self.basename = name[0..-ext.length - 1].gsub(%r!\.*\z!, "") end
Extract information from the page filename. name - The String filename of the page file. NOTE: `String#gsub` removes all trailing periods (in comparison to `String#chomp`) Returns nothing.
process
ruby
jekyll/jekyll
lib/jekyll/page.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/page.rb
MIT
def render(layouts, site_payload) site_payload["page"] = to_liquid site_payload["paginator"] = pager.to_liquid do_layout(site_payload, layouts) end
Add any necessary layouts to this post layouts - The Hash of {"name" => "layout"}. site_payload - The site payload Hash. Returns String rendered page.
render
ruby
jekyll/jekyll
lib/jekyll/page.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/page.rb
MIT
def path data.fetch("path") { relative_path } end
The path to the source file Returns the path to the source file
path
ruby
jekyll/jekyll
lib/jekyll/page.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/page.rb
MIT
def relative_path @relative_path ||= PathManager.join(@dir, @name).delete_prefix("/") end
The path to the page source file, relative to the site source
relative_path
ruby
jekyll/jekyll
lib/jekyll/page.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/page.rb
MIT
def destination(dest) @destination ||= {} @destination[dest] ||= begin path = site.in_dest_dir(dest, URL.unescape_path(url)) path = File.join(path, "index") if url.end_with?("/") path << output_ext unless path.end_with? output_ext path end end
Obtain destination path. dest - The String path to the destination dir. Returns the destination file path String.
destination
ruby
jekyll/jekyll
lib/jekyll/page.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/page.rb
MIT
def inspect "#<#{self.class} @relative_path=#{relative_path.inspect}>" end
Returns the object as a debug String.
inspect
ruby
jekyll/jekyll
lib/jekyll/page.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/page.rb
MIT
def index? basename == "index" end
Returns the Boolean of whether this Page is an index file or not.
index?
ruby
jekyll/jekyll
lib/jekyll/page.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/page.rb
MIT
def join(base, item) base = "" if base.nil? || base.empty? item = "" if item.nil? || item.empty? @join ||= {} @join[base] ||= {} @join[base][item] ||= File.join(base, item).freeze end
Wraps `File.join` to cache the frozen result. Reassigns `nil`, empty strings and empty arrays to a frozen empty string beforehand. Returns a frozen string.
join
ruby
jekyll/jekyll
lib/jekyll/path_manager.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/path_manager.rb
MIT
def sanitized_path(base_directory, questionable_path) @sanitized_path ||= {} @sanitized_path[base_directory] ||= {} @sanitized_path[base_directory][questionable_path] ||= if questionable_path.nil? base_directory.freeze else sanitize_and_join( base_directory, questionable_path ).freeze end end
Ensures the questionable path is prefixed with the base directory and prepends the questionable path with the base directory if false. Returns a frozen string.
sanitized_path
ruby
jekyll/jekyll
lib/jekyll/path_manager.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/path_manager.rb
MIT
def initialize(site) @site = site end
Create an instance of this class. site - the instance of Jekyll::Site we're concerned with Returns nothing
initialize
ruby
jekyll/jekyll
lib/jekyll/plugin_manager.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/plugin_manager.rb
MIT
def conscientious_require require_theme_deps if site.theme require_plugin_files require_gems deprecation_checks end
Require all the plugins which are allowed. Returns nothing
conscientious_require
ruby
jekyll/jekyll
lib/jekyll/plugin_manager.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/plugin_manager.rb
MIT
def require_gems Jekyll::External.require_with_graceful_fail( site.gems.select { |plugin| plugin_allowed?(plugin) } ) end
Require each of the gem plugins specified. Returns nothing.
require_gems
ruby
jekyll/jekyll
lib/jekyll/plugin_manager.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/plugin_manager.rb
MIT
def require_theme_deps return false unless site.theme.runtime_dependencies site.theme.runtime_dependencies.each do |dep| next if dep.name == "jekyll" External.require_with_graceful_fail(dep.name) if plugin_allowed?(dep.name) end end
Require each of the runtime_dependencies specified by the theme's gemspec. Returns false only if no dependencies have been specified, otherwise nothing.
require_theme_deps
ruby
jekyll/jekyll
lib/jekyll/plugin_manager.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/plugin_manager.rb
MIT
def plugin_allowed?(plugin_name) !site.safe || whitelist.include?(plugin_name) end
Check whether a gem plugin is allowed to be used during this build. plugin_name - the name of the plugin Returns true if the plugin name is in the whitelist or if the site is not in safe mode.
plugin_allowed?
ruby
jekyll/jekyll
lib/jekyll/plugin_manager.rb
https://github.com/jekyll/jekyll/blob/master/lib/jekyll/plugin_manager.rb
MIT