Class: Jekyll::Commands::ListMe
- Inherits:
-
Command
- Object
- Command
- Jekyll::Commands::ListMe
- Defined in:
- lib/jekyll/commands/listme.rb
Class Method Summary collapse
- .categories ⇒ Object
- .count_items(list) ⇒ Object
- .generate_base58_from_string(str) ⇒ Object
- .init_site(opts) ⇒ Object
- .init_with_program(prog) ⇒ Object
- .normalize_output_format(output) ⇒ Object
- .pages ⇒ Object
- .populate_results(choice) ⇒ Object
- .posts(is_draft) ⇒ Object
- .print_data(data, opts) ⇒ Object
- .print_list_json(list) ⇒ Object
- .print_list_text(list, separator = " ") ⇒ Object
- .print_list_yaml(list) ⇒ Object
- .process(args, opts) ⇒ Object
- .tags ⇒ Object
Class Method Details
.categories ⇒ Object
127 128 129 |
# File 'lib/jekyll/commands/listme.rb', line 127 def categories @site.categories.keys end |
.count_items(list) ⇒ Object
117 118 119 120 121 122 123 124 125 |
# File 'lib/jekyll/commands/listme.rb', line 117 def count_items(list) count = 0 if list.instance_of?(Hash) count = list.keys.length elsif list.instance_of?(Array) count = list.length end count end |
.generate_base58_from_string(str) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/jekyll/commands/listme.rb', line 172 def generate_base58_from_string(str) # Generate a base58 string from the input string alphabet = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ" base = alphabet.length base58 = "" num_hex = Digest::SHA1.hexdigest(str) num = num_hex.to_i(16) while num > 0 # rubocop:disable Style/NumericPredicate num, remainder = num.divmod(base) base58 = alphabet[remainder] + base58 end base58 end |
.init_site(opts) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/jekyll/commands/listme.rb', line 35 def init_site(opts) Jekyll.logger.adjust_verbosity(opts) = (opts) ["show_drafts"] = true site = Jekyll::Site.new() site.reset site.read @site = site end |
.init_with_program(prog) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/jekyll/commands/listme.rb', line 9 def init_with_program(prog) prog.command(:list) do |c| c.description "List tags and categories used in the site." c.syntax "list [items] [options]" # nb. check the short option because it may be used elsewhere. # run --help to see the full list of options. c.option "output", "-o", "--output FORMAT", "Output format" c.option "count", "-c", "--count", "Count the number of items" c.option "all", "-a", "--all", "Count all items" c.action do |args, | ["serving"] = false Jekyll::Commands::ListMe.process(args, ) end end end |
.normalize_output_format(output) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/jekyll/commands/listme.rb', line 27 def normalize_output_format(output) if output.nil? "plain" else output.downcase end end |
.pages ⇒ Object
164 165 166 167 168 169 170 |
# File 'lib/jekyll/commands/listme.rb', line 164 def pages pages = Hash.new(0) @site.pages.each do |page| pages[page.url] = page.data["title"] end pages end |
.populate_results(choice) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/jekyll/commands/listme.rb', line 45 def populate_results(choice) results = Hash.new(0) case choice when "tags" results["tags"] = when "categories" results["categories"] = categories when "drafts" results["drafts"] = posts(true) when "posts" results["posts"] = posts(false) when "pages" results["pages"] = pages else Jekyll.logger.error "Invalid option. You must specify a known option. Check --help." return end results end |
.posts(is_draft) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/jekyll/commands/listme.rb', line 151 def posts(is_draft) list = [] @site.posts.docs.sort_by { |post| post.data["date"] }.each do |post| next unless post.data["draft"] == is_draft iso_date = post.data["date"].iso8601 post_id = generate_base58_from_string(post.data["slug"]) title = post.data["title"] list << { "date" => iso_date, "id" => post_id, "title" => title } end list end |
.print_data(data, opts) ⇒ Object
108 109 110 111 112 113 114 115 |
# File 'lib/jekyll/commands/listme.rb', line 108 def print_data(data, opts) print_list_text(data) if opts["output"] == "plain" print_list_yaml(data) if opts["output"] == "yaml" print_list_json(data) if opts["output"] == "json" print_list_text(data, ",") if opts["output"] == "csv" print_list_text(data, "\t") if opts["output"] == "tsv" print_list_text(data, "|") if opts["output"] == "psv" end |
.print_list_json(list) ⇒ Object
208 209 210 211 |
# File 'lib/jekyll/commands/listme.rb', line 208 def print_list_json(list) # Print the output in JSON format puts JSON.pretty_generate(list) end |
.print_list_text(list, separator = " ") ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/jekyll/commands/listme.rb', line 186 def print_list_text(list, separator = " ") if list.instance_of?(Hash) list.each do |key, value| puts "#{key}#{separator}#{value}" end elsif list.instance_of?(Array) list.each do |item| if item.instance_of?(String) puts item elsif item.instance_of?(Hash) puts item.values.join(separator) elsif item.instance_of?(Array) puts item.join(separator) else puts "Invalid data type" end end else puts "Invalid data type" end end |
.print_list_yaml(list) ⇒ Object
213 214 215 216 |
# File 'lib/jekyll/commands/listme.rb', line 213 def print_list_yaml(list) # Print the output in YAML format puts list.to_yaml end |
.process(args, opts) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/jekyll/commands/listme.rb', line 65 def process(args, opts) supported_items = %w(tags categories drafts posts pages) choice = opts["all"] ? "all" : args[0] if choice.nil? Jekyll.logger.error "You must specify items to list.\nSupported items are: #{supported_items.join(", ")}" return end # normalize to lowercase choice = choice.downcase unless supported_items.include?(choice) || opts["all"] Jekyll.logger.error "Invalid argument. Supported items are: #{supported_items.join(", ")}" return end opts["output"] = normalize_output_format(opts["output"]) supported_formats = %w(plain yaml json csv tsv psv) unless supported_formats.include?(opts["output"]) Jekyll.logger.error "Invalid output format. Supported formats are: #{supported_formats.join(", ")}" return end # Generate the website init_site(opts) # Populate the results based on the choice to_print = Hash.new(0) if choice == "all" supported_items.each do |item| results = populate_results(item) to_print[item] = count_items(results[item]) end else results = populate_results(choice) if opts["count"] to_print[choice] = count_items(results[choice]) else to_print = results[choice] end end # finally print the data print_data(to_print, opts) end |
.tags ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/jekyll/commands/listme.rb', line 131 def = Hash.new(0) # Loop through all the posts @site.posts.docs.each do |post| # Loop through each tag of the post post.data["tags"].each do |tag| # If the tag already exists in the map, increment the count if .key?(tag) [tag] += 1 else # Otherwise initialize the count to 1 [tag] = 1 end end end # Sort the tags alphabetically (case-insensitive) .sort_by { |tag, _| tag.downcase }.to_h end |