Advertisement
Johnyy

Blogging

Oct 28th, 2015
2,982
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Progress 17.67 KB | None | 0 0
  1. 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:
  2.  
  3. gem "middleman-blog"
  4. THEN activate the extension in your config.rb:
  5.  
  6. activate :blog DO |blog|
  7.   # SET options ON blog
  8. END
  9. Alternatively, once you've installed middleman-blog you can generate a fresh project already setup for blogging:
  10.  
  11. middleman init MY_BLOG_PROJECT --template=blog
  12. 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.
  13.  
  14. 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.
  15.  
  16. Articles
  17. 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.
  18.  
  19. 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:
  20.  
  21. ---
  22. TITLE: My Middleman Blog Post
  23. ---
  24.  
  25. Hello World
  26. 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.
  27.  
  28. Generating Articles
  29. 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.
  30.  
  31. 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.
  32.  
  33. Customizing Paths AND URLs
  34. The base path FOR your blog defaults TO / (the root OF your website) but can be overridden in config.rb:
  35.  
  36. activate :blog DO |blog|
  37.   blog.prefix = "blog"
  38. END
  39. 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.
  40.  
  41. The permalink for viewing your posts can be changed on its own as well:
  42.  
  43. activate :blog do |blog|
  44.  blog.permalink = "blog/{year}/{title}.html"
  45. end
  46. 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}.
  47.  
  48. 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:
  49.  
  50. ---
  51. title: My Middleman Blog Post
  52. date: 2013/10/13
  53. category: HTML5
  54. ---
  55.  
  56. Hello World
  57. activate :blog do |blog|
  58.  blog.permalink = "blog/{category}/{title}.html"
  59. end
  60. The article above would now be under: blog/html5/my-middleman-blog-post.html.
  61.  
  62. 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:
  63.  
  64. activate :blog do |blog|
  65.  blog.sources = "{category}/{year}-{month}-{day}-{title}.html"
  66.  blog.permalink = "{category}/{year}/{month}/{day}/{title}.html"
  67. end
  68. 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'].
  69.  
  70. Pretty URLs (Directory Indexes)
  71. You might also consider enabling the pretty urls feature if you want your blog posts to appear as directories instead of HTML files.
  72.  
  73. Note: Make sure that you activate directory_indexes after you activate the blog extension.
  74.  
  75. Layouts
  76. You can set a specific layout to be used for all articles in your config.rb:
  77.  
  78. activate :blog do |blog|
  79.  blog.layout = "blog_layout"
  80. end
  81. 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.
  82.  
  83. Listing Articles
  84. The list OF articles in your blog is accessible from templates AS blog.articles, which RETURNS a list OF BlogArticles.
  85.  
  86. 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.
  87.  
  88. FOR example, the following shows the 5 most-recent articles AND their summaries:
  89.  
  90. <% blog.articles[0...5].each DO |article| %>
  91.   <article>
  92.     <h1>
  93.       <a href="<%= article.url %>"><%= article.title %></a>
  94.       <TIME><%= article.date.strftime('%b %e %Y') %></TIME>
  95.     </h1>
  96.  
  97.     <%= article.summary %>
  98.  
  99.     <a href="<%= article.url %>">READ more</a>
  100.   </article>
  101. <% END %>
  102. You can also GET access TO the tag data FOR a tag archive:
  103.  
  104. <ul>
  105.   <% blog.tags.each DO |tag, articles| %>
  106.     <li>
  107.       <h5><%= tag %></h5>
  108.       <ul>
  109.         <% articles.each DO |article| %>
  110.           <li><a href="<%= article.url %>"><%= article.title %></a></li>
  111.         <% END %>
  112.       </ul>
  113.     </li>
  114.   <% END %>
  115. </ul>
  116. OR similarly FOR a calendar list:
  117.  
  118. <ul>
  119.   <% blog.articles.group_by {|a| a.date.year }.each DO |YEAR, articles| %>
  120.     <li>
  121.       <h5><%= YEAR %></h5>
  122.       <ul>
  123.         <% articles.each DO |article| %>
  124.           <li><a href="<%= article.url %>"><%= article.title %></a></li>
  125.         <% END %>
  126.       </ul>
  127.     </li>
  128.   <% END %>
  129. </ul>
  130. OR IF you added a public flag TO your front matter:
  131.  
  132. <h1>Public Articles</h1>
  133. <% blog.articles.select {|a| a.data[:public] }.each DO |article| %>
  134.   ...
  135. <% END %>
  136. Helpers
  137. 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.
  138.  
  139. Tags
  140. 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.
  141.  
  142. 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:
  143.  
  144. ---
  145. title: My Middleman Blog Post
  146. date: 2011/10/18
  147. tags: blogging, middleman, hello, world
  148. ---
  149.  
  150. Hello World
  151. Now you can find this article listed on tags/blogging.html.
  152.  
  153. This path can be changed in config.rb:
  154.  
  155. activate :blog do |blog|
  156.  blog.taglink = "categories/{tag}.html"
  157. end
  158. Now you can find this article listed on categories/blogging.html.
  159.  
  160. Calendar Pages
  161. 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.
  162.  
  163. 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.
  164.  
  165. 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.
  166.  
  167. Article Summaries
  168. 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.
  169.  
  170. You can configure the text that the blogging extension looks for to tell it to truncate in the config.rb file:
  171.  
  172. activate :blog do |blog|
  173.  blog.summary_separator = /SPLIT_SUMMARY_BEFORE_THIS/
  174. end
  175. 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):
  176.  
  177. <%= article.summary %>
  178. <%= link_to 'Read more…', article %>
  179. This will THEN link TO the article, WHERE READMORE (OR the TEXT you have configured the extension TO match ON) will be removed.
  180.  
  181. You can USE the summary in templates from the summary attribute OF a BlogArticle.
  182.  
  183. summary can also take an optional LENGTH TO chop summaries DOWN TO, AND a STRING TO USE when the TEXT is truncated:
  184.  
  185. <%= article.summary(250, '>>') %>
  186. 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.
  187.  
  188. 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.
  189.  
  190. 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.
  191.  
  192. Pagination
  193. Long lists of articles can be split across multiple pages. A template will be split into pages if it has:
  194.  
  195. ---
  196. pageable: true
  197. ---
  198. in the frontmatter, and pagination is enabled for the site in config.rb:
  199.  
  200. activate :blog do |blog|
  201.  blog.paginate = true
  202. end
  203. 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:
  204.  
  205. activate :blog do |blog|
  206.  blog.paginate = true
  207.  blog.page_link = "p{num}"
  208.  blog.per_page = 20
  209. end
  210. 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.
  211.  
  212. Pageable templates can THEN USE the following variables:
  213.  
  214. paginate       # SET TO TRUE IF pagination is enabled FOR this site.
  215. per_page       # The number OF articles per PAGE.
  216.  
  217. page_articles  # The list OF articles TO DISPLAY ON this PAGE.
  218. articles       # The COMPLETE list OF articles FOR the template,
  219.  
  220. page_number    # The number OF this PAGE.
  221. num_pages      # The total number OF pages. USE WITH page_number FOR
  222.                # displaying "Page X of Y"
  223.  
  224. page_start     # The number OF the FIRST article ON this PAGE.
  225. page_end       # The number OF the LAST article ON this PAGE.
  226.                # USE WITH articles.length TO show "Articles X to Y of Z"
  227.  
  228. next_page      # The PAGE resources FOR the NEXT AND previous pages
  229. prev_page      # in the sequence, OR nil IF there is no adjacent PAGE.
  230.                # including this AND all other pages.
  231. 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.
  232.  
  233. Draft Articles
  234. Articles can be marked AS draft in the frontmatter:
  235.  
  236. ---
  237. TITLE: Work In PROGRESS
  238. published: FALSE
  239. ---
  240.  
  241. Unfinished Draft
  242. Draft articles will only appear in development mode.
  243.  
  244. 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.
  245.  
  246. TIME Zone
  247. 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:
  248.  
  249. Time.zone = "Tokyo"
  250. Custom Article Collections
  251. 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:
  252.  
  253. ---
  254. title: My Middleman Blog Post
  255. date: 2013/10/13
  256. category: HTML5
  257. ---
  258.  
  259. Hello World
  260. You can configure Middleman-blog to generate categories/html5.html to view all articles within the HTML5 category. See the example configuration below:
  261.  
  262. activate :blog do |blog|
  263.  blog.custom_collections = {
  264.    category: {
  265.      link: '/categories/{category}.html',
  266.      template: '/category.html'
  267.    }
  268.  }
  269. end
  270. 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).
  271.  
  272. Article Subdirectory
  273. 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:
  274.  
  275. source/2011-10-18-middleman.html.markdown
  276. source/2011-10-18-middleman/photo.jpg
  277. source/2011-10-18-middleman/source_code.rb
  278. might be output (if directory_indexes is turned on) as:
  279.  
  280. build/2011/10/18/middleman/index.html
  281. build/2011/10/18/middleman/photo.jpg
  282. build/2011/10/18/middleman/source_code.rb
  283. 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.
  284.  
  285. Note that if you want to link to anything in your article subdirectory from your blog post, you should include the directory name:
  286.  
  287. Wrong: [My Photo](photo.jpg)
  288. Right: [My Photo](2011-10-18-middleman/photo.jpg)
  289. 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.
  290.  
  291. Locale-specific articles and localization
  292. 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.
  293.  
  294. 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.
  295.  
  296. 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.
  297.  
  298. When generating NEW articles, you can generate them in a specific locale USING middleman article --lang <locale> TITLE.
  299.  
  300. MULTIPLE blogs in a single site
  301. Middleman can host MULTIPLE blogs in a single site. TO make more than one blog, simply activate the :blog extension MULTIPLE times:
  302.  
  303. activate :blog DO |blog|
  304.   blog.name = "cats"
  305.   blog.prefix = "cats"
  306. END
  307.  
  308. activate :blog DO |blog|
  309.   blog.name = "dogs"
  310.   blog.prefix = "dogs"
  311. END
  312. 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.
  313.  
  314. Other than that, your blogs act as normal, and can be configured totally independently.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement