Permalink
Browse files

Merge from metajack, wanted extended post feature.

  • Loading branch information...
2 parents 14063fe + c5d443d commit 73e3092b727811c40906c5d1162e8e1208fc2b5c @joshcarter committed Sep 27, 2011
Showing with 199 additions and 17 deletions.
  1. +5 −0 .gitignore
  2. +88 −0 emacs/jekyll.el
  3. +1 −0 jekyll.gemspec
  4. +2 −2 lib/jekyll.rb
  5. +25 −0 lib/jekyll/archive.rb
  6. +56 −9 lib/jekyll/convertible.rb
  7. +16 −1 lib/jekyll/filters.rb
  8. +3 −2 lib/jekyll/post.rb
  9. +3 −3 lib/jekyll/tags/include.rb
View
5 .gitignore
@@ -2,7 +2,12 @@ Gemfile.lock
test/dest
*.gem
pkg/
+<<<<<<< HEAD
*.swp
*~
_site/
.bundle/
+=======
+\#*
+.\#*
+>>>>>>> c5d443dc69fafcc47f8ef09c20af24b0414cca7f
View
88 emacs/jekyll.el
@@ -0,0 +1,88 @@
+;; jekyll.el
+;;
+;; Emacs support for Jekyll blogs.
+;;
+;; To use, just put this file somewhere in the load path and
+;; (require 'jekyll)
+;;
+;; Here are my key bindings:
+;; C-c b d - Show all drafts
+;; C-c b p - Show all posts
+;; C-c b n - Create new draft
+;; C-c b P - Publish current draft
+;;
+;; (global-set-key (kbd "C-c b n") 'jekyll-draft-post)
+;; (global-set-key (kbd "C-c b P") 'jekyll-publish-post)
+;; (global-set-key (kbd "C-c b p") (lambda ()
+;; (interactive)
+;; (find-file "~/Sources/blog/_posts/")))
+;; (global-set-key (kbd "C-c b d") (lambda ()
+;; (interactive)
+;; (find-file "~/Sources/blog/_drafts/")))
+
+
+(provide 'jekyll)
+
+(defvar jekyll-directory nil
+ "Path to Jekyll blog.")
+(defvar jekyll-drafts-dir "_drafts/"
+ "Relative path to drafts directory.")
+(defvar jekyll-posts-dir "_posts/"
+ "Relative path to posts directory.")
+(defvar jekyll-post-ext ".markdown"
+ "File extension of Jekyll posts.")
+(defvar jekyll-post-template
+ "---\ntitle: %s\n---\n\n"
+ "Default template for Jekyll posts. %s will be replace by the post title.")
+
+(defun jekyll-make-slug (s)
+ "Turn a string into a slug."
+ (replace-regexp-in-string
+ " " "-" (downcase
+ (replace-regexp-in-string
+ "[^A-Za-z0-9 ]" "" s))))
+
+(defun jekyll-yaml-escape (s)
+ "Escape a string for YAML."
+ (if (or (string-match ":" s)
+ (string-match "\"" s))
+ (concat "\"" (replace-regexp-in-string "\"" "\\\\\"" s) "\"")
+ s))
+
+(defun jekyll-draft-post (title)
+ "Create a new Jekyll blog post."
+ (interactive "sPost Title: ")
+ (let ((draft-file (concat jekyll-directory jekyll-drafts-dir
+ (jekyll-make-slug title)
+ jekyll-post-ext)))
+ (if (file-exists-p draft-file)
+ (find-file draft-file)
+ (find-file draft-file)
+ (insert (format jekyll-post-template (jekyll-yaml-escape title))))))
+
+(defun jekyll-publish-post ()
+ "Move a draft post to the posts directory, and rename it so that it
+contains the date."
+ (interactive)
+ (cond
+ ((not (equal
+ (file-name-directory (buffer-file-name (current-buffer)))
+ (concat jekyll-directory jekyll-drafts-dir)))
+ (message "This is not a draft post.")
+ (insert (file-name-directory (buffer-file-name (current-buffer))) "\n"
+ (concat jekyll-directory jekyll-drafts-dir)))
+ ((buffer-modified-p)
+ (message "Can't publish post; buffer has modifications."))
+ (t
+ (let ((filename
+ (concat jekyll-directory jekyll-posts-dir
+ (format-time-string "%Y-%m-%d-")
+ (file-name-nondirectory
+ (buffer-file-name (current-buffer)))))
+ (old-point (point)))
+ (rename-file (buffer-file-name (current-buffer))
+ filename)
+ (kill-buffer nil)
+ (find-file filename)
+ (set-window-point (selected-window) old-point)))))
+
View
1 jekyll.gemspec
@@ -156,4 +156,5 @@ Gem::Specification.new do |s|
# = MANIFEST =
s.test_files = s.files.select { |path| path =~ /^test\/test_.*\.rb/ }
+
end
View
4 lib/jekyll.rb
@@ -33,6 +33,7 @@ def require_all(path)
require 'jekyll/layout'
require 'jekyll/page'
require 'jekyll/post'
+require 'jekyll/archive'
require 'jekyll/filters'
require 'jekyll/static_file'
require 'jekyll/errors'
@@ -64,8 +65,7 @@ module Jekyll
'lsi' => false,
'pygments' => false,
'markdown' => 'maruku',
- 'permalink' => 'date',
-
+ 'permalink' => 'date',
'markdown_ext' => 'markdown,mkd,mkdn,md',
'textile_ext' => 'textile',
View
25 lib/jekyll/archive.rb
@@ -0,0 +1,25 @@
+module Jekyll
+
+ class Archive < Page
+ # Initialize a new Archive.
+ # +base+ is the String path to the <source>
+ # +dir+ is the String path between <source> and the file
+ #
+ # Returns <Archive>
+ def initialize(base, dir, type)
+ @base = base
+ @dir = dir
+ @name = 'index.html'
+
+ self.process(@name)
+
+ self.read_yaml(File.join(base, '_layouts'), type + '.html')
+
+ year, month, day = dir.split('/')
+ self.data['year'] = year.to_i
+ month and self.data['month'] = month.to_i
+ day and self.data['day'] = day.to_i
+ end
+ end
+
+end
View
65 lib/jekyll/convertible.rb
@@ -14,7 +14,11 @@ module Jekyll
module Convertible
# Returns the contents as a String.
def to_s
- self.content || ''
+ if self.is_a? Jekyll::Post
+ (self.content || '') + (self.extended || '')
+ else
+ self.content || ''
+ end
end
# Read the YAML frontmatter.
@@ -26,13 +30,16 @@ def to_s
def read_yaml(base, name)
self.content = File.read(File.join(base, name))
- if self.content =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)/m
- self.content = $POSTMATCH
-
- begin
- self.data = YAML.load($1)
- rescue => e
- puts "YAML Exception reading #{name}: #{e.message}"
+ if self.content =~ /^(---.*?\n.*?)\n---.*?\n(.*)/m
+ self.data = YAML.load($1)
+ self.content = $2
+
+ # if we have an extended section, separate that from content
+ if self.is_a? Jekyll::Post
+ if self.data.key? 'extended'
+ marker = self.data['extended']
+ self.content, self.extended = self.content.split(marker + "\n", 2)
+ end
end
end
@@ -43,7 +50,23 @@ def read_yaml(base, name)
#
# Returns nothing.
def transform
- self.content = converter.convert(self.content)
+
+ case self.ext
+ when ".textile":
+ converter = site.getConverterImpl(Jekyll::TextileConverter)
+ self.ext = ".html"
+ self.content = converter.convert(self.content)
+ if self.is_a? Jekyll::Post and self.extended
+ self.extended = converter.convert(self.extended).to_html
+ end
+ when ".markdown":
+ converter = site.getConverterImpl(Jekyll::MarkdownConverter)
+ self.ext = ".html"
+ self.content = converter.convert(self.content)
+ if self.is_a? Jekyll::Post and self.extended
+ self.extended = converter.convert(self.extended)
+ end
+ end
end
# Determine the extension depending on content_type.
@@ -86,6 +109,15 @@ def do_layout(payload, layouts)
# output keeps track of what will finally be written
self.output = self.content
+ if self.is_a? Jekyll::Post
+ # make sure we update the payload with transformed data
+ payload["page"].merge!({"content" => self.content, "extended" => self.extended})
+
+ if self.extended
+ self.output = self.content + self.extended
+ end
+ end
+
# recursively render layouts
layout = layouts[self.data["layout"]]
used = Set.new([layout])
@@ -108,5 +140,20 @@ def do_layout(payload, layouts)
end
end
end
+
+ # Process scripts for the layout
+ # +scripts+ is a Array of [{"name" => "foo", "command": "foo.py"}]
+ #
+ # Returns a Hash of {"foo" => "... script output ..."}
+ def do_scripts(scripts)
+ result = {}
+ scripts.each do |script|
+ p = IO.popen(File.join(@base, '_scripts', script["command"]) +
+ ' ' + @base)
+ result[script["name"]] = p.read || ""
+ p.close
+ end
+ result
+ end
end
end
View
17 lib/jekyll/filters.rb
@@ -1,7 +1,6 @@
require 'uri'
module Jekyll
-
module Filters
# Convert a Textile string into HTML output.
#
@@ -57,6 +56,10 @@ def date_to_xmlschema(date)
date.xmlschema
end
+ def date_to_utc(date)
+ date.getutc
+ end
+
def xml_escape(input)
CGI.escapeHTML(input)
end
@@ -114,5 +117,17 @@ def array_to_sentence_string(array)
end
end
+ def html_truncatewords(input, words = 15, truncate_string = "...")
+ doc = Hpricot.parse(input)
+ (doc/:"text()").to_s.split[0..words].join(' ') + truncate_string
+ end
+
+ def to_month(input)
+ return Date::MONTHNAMES[input.to_i]
+ end
+
+ def to_month_abbr(input)
+ return Date::ABBR_MONTHNAMES[input.to_i]
+ end
end
end
View
5 lib/jekyll/post.rb
@@ -19,7 +19,7 @@ def self.valid?(name)
end
attr_accessor :site
- attr_accessor :data, :content, :output, :ext
+ attr_accessor :data, :content, :output, :ext, :extended
attr_accessor :date, :slug, :published, :tags, :categories
# Initialize this Post instance.
@@ -225,7 +225,8 @@ def to_liquid
"next" => self.next,
"previous" => self.previous,
"tags" => self.tags,
- "content" => self.content })
+ "content" => self.content,
+ "extended" => self.extended || "" })
end
def inspect
View
6 lib/jekyll/tags/include.rb
@@ -1,9 +1,9 @@
module Jekyll
-
class IncludeTag < Liquid::Tag
- def initialize(tag_name, file, tokens)
+ def initialize(tag_name, markup, tokens)
super
- @file = file.strip
+
+ @template = markup.strip
end
def render(context)

0 comments on commit 73e3092

Please sign in to comment.