Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Middleman has an official extension TO support blogging, articles AND tagging. middleman-blog ships AS an extension AND must be installed TO USE. Simply specify the gem in your Gemfile:
- gem "middleman-blog"
- THEN activate the extension in your config.rb:
- activate :blog DO |blog|
- # SET options ON blog
- END
- Alternatively, once you've installed middleman-blog you can generate a fresh project already setup for blogging:
- middleman init MY_BLOG_PROJECT --template=blog
- If you already have a Middleman project, you can re-run middleman init with the blog template option to generate the sample index.html, tag.html, calendar.html, and feed.xml, or you can write those yourself. You can see what gets generated on GitHub.
- The blog extension has many configuration options - you can see what they all are and what they do by going to http://localhost:4567/__middleman/config/ in your preview server.
- Articles
- Like Middleman itself, the blog extension is focused on individual files. Each article is its own file, using any template language you like. The default filename structure for articles is {year}-{month}-{day}-{title}.html. When you want to create a new article, place it in the correct path and include some basic frontmatter to get going. You can set the blog.sources option while activating :blog in your config.rb to change where and in what format Middleman should look for articles.
- Let's say I want TO CREATE a NEW post about Middleman. I would CREATE a file AT source/2011-10-18-middleman.html.markdown. The MINIMUM contents OF this file are a TITLE ENTRY in the frontmatter:
- ---
- TITLE: My Middleman Blog Post
- ---
- Hello World
- IF you want, you can specify a full DATE AND TIME AS a DATE ENTRY in the front matter, TO HELP WITH ordering MULTIPLE posts from the same DAY. You can also include a list OF tags in the front matter TO generate tag pages.
- Generating Articles
- AS a shortcut, you can RUN middleman article TITLE AND Middleman will CREATE a NEW article FOR you in the right place WITH the right filename. You can pass --date, --lang, AND --blog options TO this command.
- IF you want TO USE your own template, you can SET blog.new_article_template TO the path (relative TO the project root) OF an ERb template that will be used BY middleman article. This template can USE the instance variables @TITLE, @slug, @DATE, AND @lang TO generate your NEW article.
- Customizing Paths AND URLs
- The base path FOR your blog defaults TO / (the root OF your website) but can be overridden in config.rb:
- activate :blog DO |blog|
- blog.prefix = "blog"
- END
- All other link settings (permalink, sources, taglink, year_link, month_link, day_link) are added ON TO prefix, so you don't need to repeat it in every setting. Note that template locations (calendar_template, year_template, month_template, day_template, and tag_template) do not get prefix added to them, so if you want them to be in the same place as your articles you should add the same prefix to those settings.
- The permalink for viewing your posts can be changed on its own as well:
- activate :blog do |blog|
- blog.permalink = "blog/{year}/{title}.html"
- end
- Now, your articles will show up at: blog/2011/blog.html. Your permalink can be totally independent from the format your posts are stored at. By default, the permalink path is {year}/{month}/{day}/{title}.html. Permalinks can be made up of any components of the article date ({year}, {month}, {day}), the title of the article (as {title}, transformed to be URL-appropriate), and {lang}.
- You can also use any other frontmatter data that is used throughout your articles. For example, if you have a category frontmatter key in your articles and wanted to include that in your permalinks:
- ---
- title: My Middleman Blog Post
- date: 2013/10/13
- category: HTML5
- ---
- Hello World
- activate :blog do |blog|
- blog.permalink = "blog/{category}/{title}.html"
- end
- The article above would now be under: blog/html5/my-middleman-blog-post.html.
- Information extracted from your blog.sources URL template can also be used in blog.permalinks. So for example if you had a config like this:
- activate :blog do |blog|
- blog.sources = "{category}/{year}-{month}-{day}-{title}.html"
- blog.permalink = "{category}/{year}/{month}/{day}/{title}.html"
- end
- you'd be able TO PUT your article sources AT cats/2013-11-12-best-cats.html AND they'd get written out to cats/2013/11/12/best-cats.html without you having to specify a category in frontmatter. You can also access the category extracted from the source path via current_article.metadata[:page]['category'].
- Pretty URLs (Directory Indexes)
- You might also consider enabling the pretty urls feature if you want your blog posts to appear as directories instead of HTML files.
- Note: Make sure that you activate directory_indexes after you activate the blog extension.
- Layouts
- You can set a specific layout to be used for all articles in your config.rb:
- activate :blog do |blog|
- blog.layout = "blog_layout"
- end
- If you want to wrap each article in a bit of structure before inserting it into a layout, you can use Middleman's NESTED layouts feature TO CREATE an article layout that is THEN wrapped WITH your main layout.
- Listing Articles
- The list OF articles in your blog is accessible from templates AS blog.articles, which RETURNS a list OF BlogArticles.
- EACH BlogArticle has some informative methods ON it, AND it is also a Resource from the sitemap which has even more information (such AS the data from your frontmatter). Within layouts AND even your articles themselves you can GET the current article via current_article.
- FOR example, the following shows the 5 most-recent articles AND their summaries:
- <% blog.articles[0...5].each DO |article| %>
- <article>
- <h1>
- <a href="<%= article.url %>"><%= article.title %></a>
- <TIME><%= article.date.strftime('%b %e %Y') %></TIME>
- </h1>
- <%= article.summary %>
- <a href="<%= article.url %>">READ more</a>
- </article>
- <% END %>
- You can also GET access TO the tag data FOR a tag archive:
- <ul>
- <% blog.tags.each DO |tag, articles| %>
- <li>
- <h5><%= tag %></h5>
- <ul>
- <% articles.each DO |article| %>
- <li><a href="<%= article.url %>"><%= article.title %></a></li>
- <% END %>
- </ul>
- </li>
- <% END %>
- </ul>
- OR similarly FOR a calendar list:
- <ul>
- <% blog.articles.group_by {|a| a.date.year }.each DO |YEAR, articles| %>
- <li>
- <h5><%= YEAR %></h5>
- <ul>
- <% articles.each DO |article| %>
- <li><a href="<%= article.url %>"><%= article.title %></a></li>
- <% END %>
- </ul>
- </li>
- <% END %>
- </ul>
- OR IF you added a public flag TO your front matter:
- <h1>Public Articles</h1>
- <% blog.articles.select {|a| a.data[:public] }.each DO |article| %>
- ...
- <% END %>
- Helpers
- There are several helpers TO USE in your templates TO make things simpler. They allow you TO DO things LIKE GET the current article, see IF the current PAGE is a blog article, OR build paths FOR tag AND calendar pages.
- Tags
- What would blogging be without organizing articles around tags? Simply add a tag ENTRY TO your articles' frontmatter. Then, you can access the tags for a BlogArticle using the tag method, and you can get a list of all tags with their associated articles from blog.tags. If you set the blog.tag_template setting in config.rb to a template (see the default config.rb) you can render a page for each tag. The tag template has the local variable tagname set to the current tag and articles set to a list of articles with that tag, and you can use the tag_path helper to generate links to a particular tag page.
- The default template produces a tag.html template for you that produces a page for each tag at tags/{tag}.html. Adding a couple tags to the above example would look like this:
- ---
- title: My Middleman Blog Post
- date: 2011/10/18
- tags: blogging, middleman, hello, world
- ---
- Hello World
- Now you can find this article listed on tags/blogging.html.
- This path can be changed in config.rb:
- activate :blog do |blog|
- blog.taglink = "categories/{tag}.html"
- end
- Now you can find this article listed on categories/blogging.html.
- Calendar Pages
- Many blogging engines produce pages that list out all articles for a specific year, month, or day. Middleman does this using a calendar.html template and the blog.calendar_template setting. The default template generates calendar.html for you. This template gets year, month, and day variables set in it, as well as articles which is a list of articles for that day.
- If you only want certain calendar pages (say, year but not day), or if you want different templates for each type of calendar page, you can set blog.year_template, blog.month_template, and blog.day_template individually. Setting blog.calendar_template is just a shortcut for setting them all to the same thing.
- In templates, you can use the blog_year_path, blog_month_path, and blog_day_path helpers to generate links to your calendar pages. You can customize what those links look like with the blog.year_link, blog.month_link, and blog.day_link settings. By default, your calendar pages will look like /2012.html, /2012/03.html, and /2012/03/15.html for year, month, and day, respectively.
- Article Summaries
- Middleman supports article truncation for cases when you'd LIKE TO show an article summary WITH a link TO the article's permalink page, such as on the homepage. The blogging extension looks for the string READMORE in your article body and shows only the content before this text on the homepage. On the permalink page, this data is then stripped out.
- You can configure the text that the blogging extension looks for to tell it to truncate in the config.rb file:
- activate :blog do |blog|
- blog.summary_separator = /SPLIT_SUMMARY_BEFORE_THIS/
- end
- You can then show just the article summary, accompanied by a link to the full article, by adding the following lines on your homepage template (or wherever you'd LIKE the summary TO appear):
- <%= article.summary %>
- <%= link_to 'Read more…', article %>
- This will THEN link TO the article, WHERE READMORE (OR the TEXT you have configured the extension TO match ON) will be removed.
- You can USE the summary in templates from the summary attribute OF a BlogArticle.
- summary can also take an optional LENGTH TO chop summaries DOWN TO, AND a STRING TO USE when the TEXT is truncated:
- <%= article.summary(250, '>>') %>
- This would produce a summary OF no more than 250 characters, followed BY ">>". The DEFAULT summary LENGTH is 250 characters - IF you wish TO DISABLE this entirely, SET blog.summary_length TO nil.
- Note that, in order TO provide HTML-aware summaries, you must add gem 'nokogiri' TO your Gemfile. This gem is NOT needed IF you're using the summary_separator (READMORE) feature and not the optional length parameter.
- If you have your own method of generating summaries, you can set blog.summary_generator to a Proc that takes the rendered blog post, desired length, and ellipsis string and produces a summary.
- Pagination
- Long lists of articles can be split across multiple pages. A template will be split into pages if it has:
- ---
- pageable: true
- ---
- in the frontmatter, and pagination is enabled for the site in config.rb:
- activate :blog do |blog|
- blog.paginate = true
- end
- By default the second and subsequent pages will have links that look like page/{num} (such as 2011/page/2.html). This can be customized, along with the default number of articles per page, in config.rb. For example:
- activate :blog do |blog|
- blog.paginate = true
- blog.page_link = "p{num}"
- blog.per_page = 20
- end
- will result in up to 20 articles per page and links that look like /2012/p2.html. The per_page parameter can also be set for an individual template in the template's frontmatter.
- Pageable templates can THEN USE the following variables:
- paginate # SET TO TRUE IF pagination is enabled FOR this site.
- per_page # The number OF articles per PAGE.
- page_articles # The list OF articles TO DISPLAY ON this PAGE.
- articles # The COMPLETE list OF articles FOR the template,
- page_number # The number OF this PAGE.
- num_pages # The total number OF pages. USE WITH page_number FOR
- # displaying "Page X of Y"
- page_start # The number OF the FIRST article ON this PAGE.
- page_end # The number OF the LAST article ON this PAGE.
- # USE WITH articles.length TO show "Articles X to Y of Z"
- next_page # The PAGE resources FOR the NEXT AND previous pages
- prev_page # in the sequence, OR nil IF there is no adjacent PAGE.
- # including this AND all other pages.
- IF paginate is FALSE AND per_page is SET in the template frontmatter, the page_articles VARIABLE will be SET TO the FIRST per_page items in articles. This simplifies the creation OF templates that can be used WITH AND without pagination enabled.
- Draft Articles
- Articles can be marked AS draft in the frontmatter:
- ---
- TITLE: Work In PROGRESS
- published: FALSE
- ---
- Unfinished Draft
- Draft articles will only appear in development mode.
- An article WITH a DATE that is in the future is also considered unpublished; IF you USE a cron job TO regenerate your site ON a regular basis, this can be used TO automatically PUBLISH articles AT a specified TIME. This behaviour can be disabled BY setting publish_future_dated TO TRUE.
- TIME Zone
- TO GET accurate publication times in your RSS feed, AND FOR automatically publishing articles ON a precise schedule, SET your blog's timezone in config.rb:
- Time.zone = "Tokyo"
- Custom Article Collections
- Middleman-Blog also supports the ability to group articles by other frontmatter data as well. A somewhat contrived example would be the ability to group articles by a category attribute:
- ---
- title: My Middleman Blog Post
- date: 2013/10/13
- category: HTML5
- ---
- Hello World
- You can configure Middleman-blog to generate categories/html5.html to view all articles within the HTML5 category. See the example configuration below:
- activate :blog do |blog|
- blog.custom_collections = {
- category: {
- link: '/categories/{category}.html',
- template: '/category.html'
- }
- }
- end
- This will configure a collection based on the category attribute. You can specify the url structure for the custom pages and the template to use when building them. When building custom collections a new helper (in this example, category_path) will be generated. This will allow you to call category_path('html5') and generate the URL categories/html5.html. The template will also automatically get a local variable with the current collection item (in this example, category).
- Article Subdirectory
- A subdirectory named according to a blog article without the extensions can be filled with files that will be copied to the right place in the build output. For example, the following directory structure:
- source/2011-10-18-middleman.html.markdown
- source/2011-10-18-middleman/photo.jpg
- source/2011-10-18-middleman/source_code.rb
- might be output (if directory_indexes is turned on) as:
- build/2011/10/18/middleman/index.html
- build/2011/10/18/middleman/photo.jpg
- build/2011/10/18/middleman/source_code.rb
- This allows files (e.g. images) that belong to a single blog article to be kept with that article in the source and in the output. Depending on your blog structure, this may make it possible to use relative links in your article, although you need to be careful if your article content is used elsewhere in your site, e.g. calendar and tag pages.
- Note that if you want to link to anything in your article subdirectory from your blog post, you should include the directory name:
- Wrong: [My Photo](photo.jpg)
- Right: [My Photo](2011-10-18-middleman/photo.jpg)
- If you don't DO this, it may still work, but other Middleman features LIKE :asset_hash won't work. See this issue for more details.
- Locale-specific articles and localization
- The blogging extension can be locale-aware. First, you'll want TO activate the :i18n extension before you activate your :blog extension. THEN, your articles will be locale-aware. This means that you can USE helpers LIKE t() inside them.
- Blog articles can have a specific language stated in their frontmatter, OR via their path USING the {lang} VARIABLE in blog.sources. FOR example, IF blog.sources was SET TO {lang}/{YEAR}-{TITLE}.html AND your article was AT source/de/2013-willkommen.html.markdown, the language FOR that article would be de. The {lang} path VARIABLE can be used in your blog.permalink path AS well.
- You can access the language OF an article USING current_article.lang, AND you can GET the list OF locale-appropriate articles in a template USING blog.local_articles.
- When generating NEW articles, you can generate them in a specific locale USING middleman article --lang <locale> TITLE.
- MULTIPLE blogs in a single site
- Middleman can host MULTIPLE blogs in a single site. TO make more than one blog, simply activate the :blog extension MULTIPLE times:
- activate :blog DO |blog|
- blog.name = "cats"
- blog.prefix = "cats"
- END
- activate :blog DO |blog|
- blog.name = "dogs"
- blog.prefix = "dogs"
- END
- This will CREATE two blogs, one AT /cats AND one AT /dogs. Notice that we give the blogs names. This is FOR helpers LIKE blog() - in order FOR them TO know what blog we want, we can pass in the blog NAME TO the blog helper LIKE blog('dogs'). In many situations Middleman will automatically be able TO FIND the right blog FOR helpers, but you may AT times need TO specify the blog NAME when USING helpers LIKE blog, tag_path, etc. You can also specify the blog NAME in a PAGE's frontmatter (such as in your tag page template) which will tell all helpers which blog to use.
- Other than that, your blogs act as normal, and can be configured totally independently.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement