Class: JekyllSort::Page
- Inherits:
-
Object
- Object
- JekyllSort::Page
- Defined in:
- lib/jekyll_sort/page.rb
Constant Summary collapse
- PREV_NEXT_MARKDOWN =
"\n{% include prev_next.md %}"
Class Method Summary collapse
Instance Method Summary collapse
- #delete_prev_next_info(page_path) ⇒ Object
- #ensure_new_prev_next_links(content) ⇒ Object
-
#initialize(link, nav_order) ⇒ Page
constructor
A new instance of Page.
- #new_content(front_matter, content) ⇒ Object
- #parse(page_path) ⇒ Object
-
#remove_old_prev_next_links(content) ⇒ Object
Hack to remove old next prev links.
- #remove_prev_next_link(content) ⇒ Object
- #update ⇒ Object
- #update_prev_next_info(page_path) ⇒ Object
Constructor Details
#initialize(link, nav_order) ⇒ Page
Returns a new instance of Page.
15 16 17 |
# File 'lib/jekyll_sort/page.rb', line 15 def initialize(link, nav_order) @link, @nav_order = link, nav_order end |
Class Method Details
.reset_all ⇒ Object
6 7 8 9 10 11 12 13 |
# File 'lib/jekyll_sort/page.rb', line 6 def self.reset_all paths = Dir.glob("**/**.md") paths.each do |path| link = path.sub('_','/').sub(/\.md$/,'/') # _docs/dsl.md => /docs/dsl/ link = "/#{link}" unless link =~ /^\// # quick-start => /quick-start Page.new(link, :delete).update end end |
Instance Method Details
#delete_prev_next_info(page_path) ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/jekyll_sort/page.rb', line 56 def delete_prev_next_info(page_path) front_matter, parsed = parse(page_path) return if front_matter.empty? # only add to front matter if there already a front matter front_matter.delete("nav_order") content = new_content(front_matter, parsed.content) content = remove_prev_next_link(content) content = remove_old_prev_next_links(content) end |
#ensure_new_prev_next_links(content) ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/jekyll_sort/page.rb', line 83 def ensure_new_prev_next_links(content) if content.include?(PREV_NEXT_MARKDOWN) content else content + "\n" + PREV_NEXT_MARKDOWN end end |
#new_content(front_matter, content) ⇒ Object
66 67 68 69 |
# File 'lib/jekyll_sort/page.rb', line 66 def new_content(front_matter, content) new_front_matter = YAML.dump(front_matter) "#{new_front_matter}---\n\n#{content}" end |
#parse(page_path) ⇒ Object
71 72 73 74 75 |
# File 'lib/jekyll_sort/page.rb', line 71 def parse(page_path) parsed = FrontMatterParser::Parser.parse_file(page_path) front_matter = parsed.front_matter [front_matter, parsed] end |
#remove_old_prev_next_links(content) ⇒ Object
Hack to remove old next prev links. Just always call it for old sites.
92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/jekyll_sort/page.rb', line 92 def remove_old_prev_next_links(content) result = [] lines = content.split("\n") lines.each do |l| unless l.include?('<a id="prev" ') || l.include?('<a id="next" ') || l.include?('<p class="keyboard-tip') result << l end end result.join("\n") end |
#remove_prev_next_link(content) ⇒ Object
79 80 81 |
# File 'lib/jekyll_sort/page.rb', line 79 def remove_prev_next_link(content) content.sub(PREV_NEXT_MARKDOWN, '') end |
#update ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/jekyll_sort/page.rb', line 19 def update return if @link =~ /reference/ || @link =~ /includes/ || @link =~ /site/ page_path = @link.sub('/','').sub(/\/$/,'') + ".md" # remove leading / page_path = "_#{page_path}" if page_path =~ /^docs\// # puts "@link #{@link}" # puts "page_path #{page_path}" unless File.exist?(page_path) puts "WARN: path #{page_path} not found" return end if @nav_order == :delete puts "Deleting prev next info: #{page_path}" content = delete_prev_next_info(page_path) else puts "Updating #{page_path} with nav_order #@nav_order" content = update_prev_next_info(page_path) end return unless content IO.write(page_path, content) end |
#update_prev_next_info(page_path) ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/jekyll_sort/page.rb', line 47 def update_prev_next_info(page_path) front_matter, parsed = parse(page_path) return if front_matter.empty? # only add to front matter if there already a front matter front_matter["nav_order"] = @nav_order content = new_content(front_matter, parsed.content) content = ensure_new_prev_next_links(content) end |