diff options
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/category_generator.rb | 3 | ||||
-rw-r--r-- | plugins/code_block.rb | 12 | ||||
-rw-r--r-- | plugins/include_code.rb | 11 | ||||
-rw-r--r-- | plugins/octopress_filters.rb | 20 | ||||
-rw-r--r-- | plugins/pagination.rb | 121 | ||||
-rw-r--r-- | plugins/pygments_code.rb | 21 |
6 files changed, 164 insertions, 24 deletions
diff --git a/plugins/category_generator.rb b/plugins/category_generator.rb index 205022d3..aa1180e1 100644 --- a/plugins/category_generator.rb +++ b/plugins/category_generator.rb @@ -104,9 +104,8 @@ module Jekyll # def category_links(categories) dir = @context.registers[:site].config['category_dir'] - root_url = @context.registers[:site].config['root'].sub(/\/$/, '') categories = categories.sort!.map do |item| - "<a class='category' href='#{root_url}/#{dir}/#{item.gsub(/_|\W/, '-')}/'>#{item}</a>" + "<a class='category' href='/#{dir}/#{item.gsub(/_|\W/, '-')}/'>#{item}</a>" end case categories.length diff --git a/plugins/code_block.rb b/plugins/code_block.rb index ff242aab..9c971bf5 100644 --- a/plugins/code_block.rb +++ b/plugins/code_block.rb @@ -53,7 +53,12 @@ module Jekyll def initialize(tag_name, markup, tokens) @title = nil @caption = nil + @filetype = nil @highlight = true + if markup =~ /\s*lang:(\w+)/i + @filetype = $1 + markup = markup.sub(/lang:\w+/i,'') + end if markup =~ CaptionUrlTitle @file = $1 @caption = "<figcaption><span>#{$1}</span><a href='#{$2 + $3}'>#{$4}</a></figcaption>" @@ -64,7 +69,7 @@ module Jekyll @file = $1 @caption = "<figcaption><span>#{$1}</span></figcaption>\n" end - if @file =~ /\S[\S\s]*\.(\w+)/ + if @file =~ /\S[\S\s]*\w+\.(\w+)/ && @filetype.nil? @filetype = $1 end super @@ -77,12 +82,9 @@ module Jekyll source += @caption if @caption source = context['pygments_prefix'] + source if context['pygments_prefix'] if @filetype - @filetype = 'objc' if @filetype == 'm' - @filetype = 'perl' if @filetype == 'pl' - @filetype = 'yaml' if @filetype == 'yml' source += " #{highlight(code, @filetype)}</figure></div>" else - source += "<pre><code>" + code.lstrip.rstrip.gsub(/</,'<') + "</code></pre></figure></div>" + source += "#{tableize_code(code.lstrip.rstrip.gsub(/</,'<'))}</figure></div>" end source = source + context['pygments_suffix'] if context['pygments_suffix'] end diff --git a/plugins/include_code.rb b/plugins/include_code.rb index e064fe2c..70d5f138 100644 --- a/plugins/include_code.rb +++ b/plugins/include_code.rb @@ -30,6 +30,10 @@ module Jekyll def initialize(tag_name, markup, tokens) @title = nil @file = nil + if markup.strip =~ /\s*lang:(\w+)/i + @filetype = $1 + markup = markup.strip.sub(/lang:\w+/i,'') + end if markup.strip =~ /(.*)?(\s+|^)(\/*\S+)/i @title = $1 || nil @file = $3 @@ -52,12 +56,9 @@ module Jekyll Dir.chdir(code_path) do code = file.read - @filetype = file.extname.sub('.','') - @filetype = 'objc' if @filetype == 'm' - @filetype = 'perl' if @filetype == 'pl' - @filetype = 'yaml' if @filetype == 'yml' + @filetype = file.extname.sub('.','') if @filetype.nil? title = @title ? "#{@title} (#{file.basename})" : file.basename - url = "#{context.registers[:site].config['url']}/#{code_dir}/#{@file}" + url = "/#{code_dir}/#{@file}" source = "<div><figure role=code><figcaption><span>#{title}</span> <a href='#{url}'>download</a></figcaption>\n" source += " #{highlight(code, @filetype)}</figure></div>" end diff --git a/plugins/octopress_filters.rb b/plugins/octopress_filters.rb index 67118b53..a63c43ab 100644 --- a/plugins/octopress_filters.rb +++ b/plugins/octopress_filters.rb @@ -12,6 +12,11 @@ module OctopressFilters end end + # Checks for excerpts (helpful for template conditionals) + def has_excerpt(input) + input =~ /<!--\s*more\s*-->/i ? true : false + end + # Summary is used on the Archive pages to return the first block of content from a post. def summary(input) if input.index(/\n\n/) @@ -26,14 +31,17 @@ module OctopressFilters # code snippet # ``` def backtick_codeblock(input) + code = nil # Markdown support input = input.gsub /<p>`{3}\s*(\w+)?<\/p>\s*<pre><code>\s*(.+?)\s*<\/code><\/pre>\s*<p>`{3}<\/p>/m do lang = $1 if lang != '' str = $2.gsub('<','<').gsub('>','>').gsub('&','&') - highlight(str, lang) + code = highlight(str, lang) + "<figure role=code>#{code}</figure>" else - "<pre><code>#{$2}</code></pre>" + code = tableize_code($2) + "<figure role=code>#{code}</figure>" end end @@ -48,9 +56,11 @@ module OctopressFilters lang = $1 str = $2.gsub(/^\s{4}/, '') if lang != '' - highlight(str, lang) + code = highlight(str, lang) + "<figure role=code>#{code}</figure>" else - "<pre><code>#{$2.gsub('<','<').gsub('>','>')}</code></pre>" + code = tableize_code($2.gsub('<','<').gsub('>','>')) + "<figure role=code>#{code}</figure>" end end end @@ -58,7 +68,7 @@ module OctopressFilters # Replaces relative urls with full urls def expand_urls(input, url='') url ||= '/' - input.gsub /(\s+(href|src)\s*=\s*["|']{1})(\/[^\"'>]+)/ do + input.gsub /(\s+(href|src)\s*=\s*["|']{1})(\/[^\"'>]*)/ do $1+url+$3 end end diff --git a/plugins/pagination.rb b/plugins/pagination.rb new file mode 100644 index 00000000..a318754e --- /dev/null +++ b/plugins/pagination.rb @@ -0,0 +1,121 @@ +module Jekyll + + class Pagination < Generator + # This generator is safe from arbitrary code execution. + safe true + + # Generate paginated pages if necessary. + # + # site - The Site. + # + # Returns nothing. + def generate(site) + site.pages.dup.each do |page| + paginate(site, page) if Pager.pagination_enabled?(site.config, page) + end + end + + # Paginates the blog's posts. Renders the index.html file into paginated + # directories, e.g.: page2/index.html, page3/index.html, etc and adds more + # site-wide data. + # + # site - The Site. + # page - The index.html Page that requires pagination. + # + # {"paginator" => { "page" => <Number>, + # "per_page" => <Number>, + # "posts" => [<Post>], + # "total_posts" => <Number>, + # "total_pages" => <Number>, + # "previous_page" => <Number>, + # "next_page" => <Number> }} + def paginate(site, page) + all_posts = site.site_payload['site']['posts'] + pages = Pager.calculate_pages(all_posts, site.config['paginate'].to_i) + page_dir = page.destination('').sub(/\/[^\/]+$/, '') + page_dir_config = site.config['pagination_dir'] + dir = ((page_dir_config || page_dir) + '/').sub(/^\/+/, '') + + (1..pages).each do |num_page| + pager = Pager.new(site.config, num_page, all_posts, page_dir+'/', '/'+dir, pages) + if num_page > 1 + newpage = Page.new(site, site.source, page_dir, page.name) + newpage.pager = pager + newpage.dir = File.join(page.dir, "#{dir}page/#{num_page}") + site.pages << newpage + else + page.pager = pager + end + end + end + end + + class Pager + attr_reader :page, :per_page, :posts, :total_posts, :total_pages, :previous_page, :next_page + + # Calculate the number of pages. + # + # all_posts - The Array of all Posts. + # per_page - The Integer of entries per page. + # + # Returns the Integer number of pages. + def self.calculate_pages(all_posts, per_page) + (all_posts.size.to_f / per_page.to_i).ceil + end + + # Determine if pagination is enabled for a given file. + # + # config - The configuration Hash. + # file - The String filename of the file. + # + # Returns true if pagination is enabled, false otherwise. + def self.pagination_enabled?(config, file) + file.name == 'index.html' && !config['paginate'].nil? && file.content =~ /paginator\./ + end + + # Initialize a new Pager. + # + # config - The Hash configuration of the site. + # page - The Integer page number. + # all_posts - The Array of all the site's Posts. + # num_pages - The Integer number of pages or nil if you'd like the number + # of pages calculated. + def initialize(config, page, all_posts, index_dir, pagination_dir, num_pages = nil) + @page = page + @per_page = config['paginate'].to_i + @page_dir = pagination_dir + 'page/' + @total_pages = num_pages || Pager.calculate_pages(all_posts, @per_page) + @previous_page = nil + + if @page > @total_pages + raise RuntimeError, "page number can't be greater than total pages: #{@page} > #{@total_pages}" + end + + init = (@page - 1) * @per_page + offset = (init + @per_page - 1) >= all_posts.size ? all_posts.size : (init + @per_page - 1) + + @total_posts = all_posts.size + @posts = all_posts[init..offset] + @previous_page = @page != 1 ? @page_dir + (@page - 1).to_s + '/' : nil + @previous_page = index_dir if @page - 1 == 1 + @next_page = @page != @total_pages ? @page_dir + (@page + 1).to_s + '/' : nil + end + + # Convert this Pager's data to a Hash suitable for use by Liquid. + # + # Returns the Hash representation of this Pager. + def to_liquid + { + 'page' => page, + 'per_page' => per_page, + 'posts' => posts, + 'total_posts' => total_posts, + 'total_pages' => total_pages, + 'previous_page' => previous_page, + 'next_page' => next_page + } + end + end + +end + diff --git a/plugins/pygments_code.rb b/plugins/pygments_code.rb index cdd6c3a4..67ce8c34 100644 --- a/plugins/pygments_code.rb +++ b/plugins/pygments_code.rb @@ -7,14 +7,12 @@ FileUtils.mkdir_p(PYGMENTS_CACHE_DIR) module HighlightCode def highlight(str, lang) + lang = 'ruby' if lang == 'ru' + lang = 'objc' if lang == 'm' + lang = 'perl' if lang == 'pl' + lang = 'yaml' if lang == 'yml' str = pygments(str, lang).match(/<pre>(.+)<\/pre>/m)[1].to_s.gsub(/ *$/, '') #strip out divs <div class="highlight"> - table = '<div class="highlight"><table cellpadding="0" cellspacing="0"><tr><td class="gutter"><pre class="line-numbers">' - code = '' - str.lines.each_with_index do |line,index| - table += "<span class='line'>#{index+1}</span>\n" - code += "<div class='line'>#{line}</div>" - end - table += "</pre></td><td class='code' width='100%'><pre><code class='#{lang}'>#{code}</code></pre></td></tr></table></div>" + tableize_code(str, lang) end def pygments(code, lang) @@ -31,4 +29,13 @@ module HighlightCode end highlighted_code end + def tableize_code (str, lang = '') + table = '<div class="highlight"><table cellpadding="0" cellspacing="0"><tr><td class="gutter"><pre class="line-numbers">' + code = '' + str.lines.each_with_index do |line,index| + table += "<span class='line'>#{index+1}</span>\n" + code += "<div class='line'>#{line}</div>" + end + table += "</pre></td><td class='code' width='100%'><pre><code class='#{lang}'>#{code}</code></pre></td></tr></table></div>" + end end |