moft 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. data/Gemfile +2 -0
  2. data/LICENSE +21 -0
  3. data/bin/moft +83 -0
  4. data/lib/moft.rb +89 -0
  5. data/lib/moft/command.rb +27 -0
  6. data/lib/moft/commands/build.rb +64 -0
  7. data/lib/moft/commands/new.rb +50 -0
  8. data/lib/moft/commands/serve.rb +33 -0
  9. data/lib/moft/configuration.rb +171 -0
  10. data/lib/moft/converter.rb +48 -0
  11. data/lib/moft/converters/identity.rb +21 -0
  12. data/lib/moft/converters/markdown.rb +43 -0
  13. data/lib/moft/converters/markdown/kramdown_parser.rb +44 -0
  14. data/lib/moft/converters/markdown/maruku_parser.rb +47 -0
  15. data/lib/moft/converters/markdown/rdiscount_parser.rb +26 -0
  16. data/lib/moft/converters/markdown/redcarpet_parser.rb +40 -0
  17. data/lib/moft/converters/textile.rb +50 -0
  18. data/lib/moft/convertible.rb +152 -0
  19. data/lib/moft/core_ext.rb +68 -0
  20. data/lib/moft/deprecator.rb +34 -0
  21. data/lib/moft/draft.rb +35 -0
  22. data/lib/moft/errors.rb +4 -0
  23. data/lib/moft/filters.rb +141 -0
  24. data/lib/moft/generator.rb +4 -0
  25. data/lib/moft/generators/pagination.rb +131 -0
  26. data/lib/moft/layout.rb +42 -0
  27. data/lib/moft/logger.rb +52 -0
  28. data/lib/moft/mime.types +85 -0
  29. data/lib/moft/page.rb +147 -0
  30. data/lib/moft/plugin.rb +75 -0
  31. data/lib/moft/post.rb +377 -0
  32. data/lib/moft/site.rb +422 -0
  33. data/lib/moft/static_file.rb +70 -0
  34. data/lib/moft/tags/gist.rb +30 -0
  35. data/lib/moft/tags/highlight.rb +83 -0
  36. data/lib/moft/tags/include.rb +37 -0
  37. data/lib/moft/tags/post_url.rb +46 -0
  38. data/lib/site_template/_config.yml +1 -0
  39. data/lib/site_template/_layouts/default.html +38 -0
  40. data/lib/site_template/_layouts/post.html +6 -0
  41. data/lib/site_template/_posts/0000-00-00-welcome-to-jekyll.markdown.erb +24 -0
  42. data/lib/site_template/css/screen.css +189 -0
  43. data/lib/site_template/css/syntax.css +60 -0
  44. data/lib/site_template/images/.gitkeep +0 -0
  45. data/lib/site_template/images/rss.png +0 -0
  46. data/lib/site_template/index.html +13 -0
  47. data/moft.gemspec +100 -0
  48. metadata +412 -0
@@ -0,0 +1,68 @@
1
+ class Hash
2
+ # Merges self with another hash, recursively.
3
+ #
4
+ # This code was lovingly stolen from some random gem:
5
+ # http://gemjack.com/gems/tartan-0.1.1/classes/Hash.html
6
+ #
7
+ # Thanks to whoever made it.
8
+ def deep_merge(hash)
9
+ target = dup
10
+
11
+ hash.keys.each do |key|
12
+ if hash[key].is_a? Hash and self[key].is_a? Hash
13
+ target[key] = target[key].deep_merge(hash[key])
14
+ next
15
+ end
16
+
17
+ target[key] = hash[key]
18
+ end
19
+
20
+ target
21
+ end
22
+
23
+ # Read array from the supplied hash favouring the singular key
24
+ # and then the plural key, and handling any nil entries.
25
+ # +hash+ the hash to read from
26
+ # +singular_key+ the singular key
27
+ # +plural_key+ the plural key
28
+ #
29
+ # Returns an array
30
+ def pluralized_array(singular_key, plural_key)
31
+ hash = self
32
+ if hash.has_key?(singular_key)
33
+ array = [hash[singular_key]] if hash[singular_key]
34
+ elsif hash.has_key?(plural_key)
35
+ case hash[plural_key]
36
+ when String
37
+ array = hash[plural_key].split
38
+ when Array
39
+ array = hash[plural_key].compact
40
+ end
41
+ end
42
+ array || []
43
+ end
44
+ end
45
+
46
+ # Thanks, ActiveSupport!
47
+ class Date
48
+ # Converts datetime to an appropriate format for use in XML
49
+ def xmlschema
50
+ strftime("%Y-%m-%dT%H:%M:%S%Z")
51
+ end if RUBY_VERSION < '1.9'
52
+ end
53
+
54
+ module Enumerable
55
+ # Returns true if path matches against any glob pattern.
56
+ # Look for more detail about glob pattern in method File::fnmatch.
57
+ def glob_include?(e)
58
+ any? { |exp| File.fnmatch?(exp, e) }
59
+ end
60
+ end
61
+
62
+ if RUBY_VERSION < "1.9"
63
+ class String
64
+ def force_encoding(enc)
65
+ self
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,34 @@
1
+ module Moft
2
+ class Deprecator
3
+ def self.process(args)
4
+ no_subcommand(args)
5
+ deprecation_message args, "--server", "The --server command has been replaced by the \
6
+ 'serve' subcommand."
7
+ deprecation_message args, "--no-server", "To build Moft without launching a server, \
8
+ use the 'build' subcommand."
9
+ deprecation_message args, "--auto", "The switch '--auto' has been replaced with '--watch'."
10
+ deprecation_message args, "--no-auto", "To disable auto-replication, simply leave off \
11
+ the '--watch' switch."
12
+ deprecation_message args, "--pygments", "The 'pygments' setting can only be set in \
13
+ your config files."
14
+ deprecation_message args, "--paginate", "The 'paginate' setting can only be set in your \
15
+ config files."
16
+ deprecation_message args, "--url", "The 'url' setting can only be set in your config files."
17
+ end
18
+
19
+ def self.no_subcommand(args)
20
+ if args.size == 0 || args.first =~ /^--/
21
+ Moft::Logger.error "Deprecation:", "Moft now uses subcommands instead of just \
22
+ switches. Run `moft help' to find out more."
23
+ exit(1)
24
+ end
25
+ end
26
+
27
+ def self.deprecation_message(args, deprecated_argument, message)
28
+ if args.include?(deprecated_argument)
29
+ Moft::Logger.error "Deprecation:", message
30
+ exit(1)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,35 @@
1
+ module Moft
2
+
3
+ class Draft < Post
4
+
5
+ # Valid post name regex (no date)
6
+ MATCHER = /^(.*)(\.[^.]+)$/
7
+
8
+ # Draft name validator. Draft filenames must be like:
9
+ # my-awesome-post.textile
10
+ #
11
+ # Returns true if valid, false if not.
12
+ def self.valid?(name)
13
+ name =~ MATCHER
14
+ end
15
+
16
+ # Get the full path to the directory containing the draft files
17
+ def containing_dir(source, dir)
18
+ File.join(source, dir, '_drafts')
19
+ end
20
+
21
+ # Extract information from the post filename.
22
+ #
23
+ # name - The String filename of the post file.
24
+ #
25
+ # Returns nothing.
26
+ def process(name)
27
+ m, slug, ext = *name.match(MATCHER)
28
+ self.date = File.mtime(File.join(@base, name))
29
+ self.slug = slug
30
+ self.ext = ext
31
+ end
32
+
33
+ end
34
+
35
+ end
@@ -0,0 +1,4 @@
1
+ module Moft
2
+ class FatalException < StandardError
3
+ end
4
+ end
@@ -0,0 +1,141 @@
1
+ require 'uri'
2
+
3
+ module Moft
4
+ module Filters
5
+ # Convert a Textile string into HTML output.
6
+ #
7
+ # input - The Textile String to convert.
8
+ #
9
+ # Returns the HTML formatted String.
10
+ def textilize(input)
11
+ site = @context.registers[:site]
12
+ converter = site.getConverterImpl(Moft::Converters::Textile)
13
+ converter.convert(input)
14
+ end
15
+
16
+ # Convert a Markdown string into HTML output.
17
+ #
18
+ # input - The Markdown String to convert.
19
+ #
20
+ # Returns the HTML formatted String.
21
+ def markdownify(input)
22
+ site = @context.registers[:site]
23
+ converter = site.getConverterImpl(Moft::Converters::Markdown)
24
+ converter.convert(input)
25
+ end
26
+
27
+ # Format a date in short format e.g. "27 Jan 2011".
28
+ #
29
+ # date - the Time to format.
30
+ #
31
+ # Returns the formatting String.
32
+ def date_to_string(date)
33
+ date.strftime("%d %b %Y")
34
+ end
35
+
36
+ # Format a date in long format e.g. "27 January 2011".
37
+ #
38
+ # date - The Time to format.
39
+ #
40
+ # Returns the formatted String.
41
+ def date_to_long_string(date)
42
+ date.strftime("%d %B %Y")
43
+ end
44
+
45
+ # Format a date for use in XML.
46
+ #
47
+ # date - The Time to format.
48
+ #
49
+ # Examples
50
+ #
51
+ # date_to_xmlschema(Time.now)
52
+ # # => "2011-04-24T20:34:46+08:00"
53
+ #
54
+ # Returns the formatted String.
55
+ def date_to_xmlschema(date)
56
+ date.xmlschema
57
+ end
58
+
59
+ # Format a date according to RFC-822
60
+ #
61
+ # date - The Time to format.
62
+ #
63
+ # Examples
64
+ #
65
+ # date_to_rfc822(Time.now)
66
+ # # => "Sun, 24 Apr 2011 12:34:46 +0000"
67
+ #
68
+ # Returns the formatted String.
69
+ def date_to_rfc822(date)
70
+ date.rfc822
71
+ end
72
+
73
+ # XML escape a string for use. Replaces any special characters with
74
+ # appropriate HTML entity replacements.
75
+ #
76
+ # input - The String to escape.
77
+ #
78
+ # Examples
79
+ #
80
+ # xml_escape('foo "bar" <baz>')
81
+ # # => "foo &quot;bar&quot; &lt;baz&gt;"
82
+ #
83
+ # Returns the escaped String.
84
+ def xml_escape(input)
85
+ CGI.escapeHTML(input)
86
+ end
87
+
88
+ # CGI escape a string for use in a URL. Replaces any special characters
89
+ # with appropriate %XX replacements.
90
+ #
91
+ # input - The String to escape.
92
+ #
93
+ # Examples
94
+ #
95
+ # cgi_escape('foo,bar;baz?')
96
+ # # => "foo%2Cbar%3Bbaz%3F"
97
+ #
98
+ # Returns the escaped String.
99
+ def cgi_escape(input)
100
+ CGI::escape(input)
101
+ end
102
+
103
+ def uri_escape(input)
104
+ URI.escape(input)
105
+ end
106
+
107
+ # Count the number of words in the input string.
108
+ #
109
+ # input - The String on which to operate.
110
+ #
111
+ # Returns the Integer word count.
112
+ def number_of_words(input)
113
+ input.split.length
114
+ end
115
+
116
+ # Join an array of things into a string by separating with commes and the
117
+ # word "and" for the last one.
118
+ #
119
+ # array - The Array of Strings to join.
120
+ #
121
+ # Examples
122
+ #
123
+ # array_to_sentence_string(["apples", "oranges", "grapes"])
124
+ # # => "apples, oranges, and grapes"
125
+ #
126
+ # Returns the formatted String.
127
+ def array_to_sentence_string(array)
128
+ connector = "and"
129
+ case array.length
130
+ when 0
131
+ ""
132
+ when 1
133
+ array[0].to_s
134
+ when 2
135
+ "#{array[0]} #{connector} #{array[1]}"
136
+ else
137
+ "#{array[0...-1].join(', ')}, #{connector} #{array[-1]}"
138
+ end
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,4 @@
1
+ module Moft
2
+ class Generator < Plugin
3
+ end
4
+ end
@@ -0,0 +1,131 @@
1
+ module Moft
2
+ module Generators
3
+ class Pagination < Generator
4
+ # This generator is safe from arbitrary code execution.
5
+ safe true
6
+
7
+ # Generate paginated pages if necessary.
8
+ #
9
+ # site - The Site.
10
+ #
11
+ # Returns nothing.
12
+ def generate(site)
13
+ site.pages.dup.each do |page|
14
+ paginate(site, page) if Pager.pagination_enabled?(site.config, page.name)
15
+ end
16
+ end
17
+
18
+ # Paginates the blog's posts. Renders the index.html file into paginated
19
+ # directories, e.g.: page2/index.html, page3/index.html, etc and adds more
20
+ # site-wide data.
21
+ #
22
+ # site - The Site.
23
+ # page - The index.html Page that requires pagination.
24
+ #
25
+ # {"paginator" => { "page" => <Number>,
26
+ # "per_page" => <Number>,
27
+ # "posts" => [<Post>],
28
+ # "total_posts" => <Number>,
29
+ # "total_pages" => <Number>,
30
+ # "previous_page" => <Number>,
31
+ # "next_page" => <Number> }}
32
+ def paginate(site, page)
33
+ all_posts = site.site_payload['site']['posts']
34
+ pages = Pager.calculate_pages(all_posts, site.config['paginate'].to_i)
35
+ (1..pages).each do |num_page|
36
+ pager = Pager.new(site.config, num_page, all_posts, pages)
37
+ if num_page > 1
38
+ newpage = Page.new(site, site.source, page.dir, page.name)
39
+ newpage.pager = pager
40
+ newpage.dir = File.join(page.dir, Pager.paginate_path(site.config, num_page))
41
+ site.pages << newpage
42
+ else
43
+ page.pager = pager
44
+ end
45
+ end
46
+ end
47
+
48
+ end
49
+ end
50
+
51
+ class Pager
52
+ attr_reader :page, :per_page, :posts, :total_posts, :total_pages,
53
+ :previous_page, :previous_page_path, :next_page, :next_page_path
54
+
55
+ # Calculate the number of pages.
56
+ #
57
+ # all_posts - The Array of all Posts.
58
+ # per_page - The Integer of entries per page.
59
+ #
60
+ # Returns the Integer number of pages.
61
+ def self.calculate_pages(all_posts, per_page)
62
+ (all_posts.size.to_f / per_page.to_i).ceil
63
+ end
64
+
65
+ # Determine if pagination is enabled for a given file.
66
+ #
67
+ # config - The configuration Hash.
68
+ # file - The String filename of the file.
69
+ #
70
+ # Returns true if pagination is enabled, false otherwise.
71
+ def self.pagination_enabled?(config, file)
72
+ file == 'index.html' && !config['paginate'].nil?
73
+ end
74
+
75
+ # Static: Return the pagination path of the page
76
+ #
77
+ # site_config - the site config
78
+ # num_page - the pagination page number
79
+ #
80
+ # Returns the pagination path as a string
81
+ def self.paginate_path(site_config, num_page)
82
+ return nil if num_page.nil? || num_page <= 1
83
+ format = site_config['paginate_path']
84
+ format.sub(':num', num_page.to_s)
85
+ end
86
+
87
+ # Initialize a new Pager.
88
+ #
89
+ # config - The Hash configuration of the site.
90
+ # page - The Integer page number.
91
+ # all_posts - The Array of all the site's Posts.
92
+ # num_pages - The Integer number of pages or nil if you'd like the number
93
+ # of pages calculated.
94
+ def initialize(config, page, all_posts, num_pages = nil)
95
+ @page = page
96
+ @per_page = config['paginate'].to_i
97
+ @total_pages = num_pages || Pager.calculate_pages(all_posts, @per_page)
98
+
99
+ if @page > @total_pages
100
+ raise RuntimeError, "page number can't be greater than total pages: #{@page} > #{@total_pages}"
101
+ end
102
+
103
+ init = (@page - 1) * @per_page
104
+ offset = (init + @per_page - 1) >= all_posts.size ? all_posts.size : (init + @per_page - 1)
105
+
106
+ @total_posts = all_posts.size
107
+ @posts = all_posts[init..offset]
108
+ @previous_page = @page != 1 ? @page - 1 : nil
109
+ @previous_page_path = Pager.paginate_path(config, @previous_page)
110
+ @next_page = @page != @total_pages ? @page + 1 : nil
111
+ @next_page_path = Pager.paginate_path(config, @next_page)
112
+ end
113
+
114
+ # Convert this Pager's data to a Hash suitable for use by Liquid.
115
+ #
116
+ # Returns the Hash representation of this Pager.
117
+ def to_liquid
118
+ {
119
+ 'page' => page,
120
+ 'per_page' => per_page,
121
+ 'posts' => posts,
122
+ 'total_posts' => total_posts,
123
+ 'total_pages' => total_pages,
124
+ 'previous_page' => previous_page,
125
+ 'previous_page_path' => previous_page_path,
126
+ 'next_page' => next_page,
127
+ 'next_page_path' => next_page_path
128
+ }
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,42 @@
1
+ module Moft
2
+ class Layout
3
+ include Convertible
4
+
5
+ # Gets the Site object.
6
+ attr_reader :site
7
+
8
+ # Gets/Sets the extension of this layout.
9
+ attr_accessor :ext
10
+
11
+ # Gets/Sets the Hash that holds the metadata for this layout.
12
+ attr_accessor :data
13
+
14
+ # Gets/Sets the content of this layout.
15
+ attr_accessor :content
16
+
17
+ # Initialize a new Layout.
18
+ #
19
+ # site - The Site.
20
+ # base - The String path to the source.
21
+ # name - The String filename of the post file.
22
+ def initialize(site, base, name)
23
+ @site = site
24
+ @base = base
25
+ @name = name
26
+
27
+ self.data = {}
28
+
29
+ self.process(name)
30
+ self.read_yaml(base, name)
31
+ end
32
+
33
+ # Extract information from the layout filename.
34
+ #
35
+ # name - The String filename of the layout file.
36
+ #
37
+ # Returns nothing.
38
+ def process(name)
39
+ self.ext = File.extname(name)
40
+ end
41
+ end
42
+ end