Publish Multi Files

gommmmmm edited this page Sep 13, 2010 · 17 revisions

Publish multiple files

With SmallCage 0.1.8 or later, you can modify the name of output file programmatically. Also you can generate multiple files from a single smc source file.

URI template

Usually a single smc file (ex. index.html.smc) will be converted into a single output file (ex. index.html). You can change this rule with URI template.

File layout

URI template is a file named like ‘(template name).uri.rhtml’. It should be placed under templates directory as other template files.

  • /_smc/templates/page.uri.rhtml

URI template is used in addition to normal template. In example above, page.url.rhtml must be used with a template named ‘page.rhtml’.

Format of URI template

  • URI templates will be processed by SmallCage, just like other normal template.
  • Put one URI per line – URI for the output files.
  • Or write ERB to output a list of URI (not a instance of List. A string contains one URI per line).
  • Lines start with ‘/’ means a path from document root.
  • Lines not start with ‘/’ means a relative path from smc file
  • You can’t output files outside document root.

Examples

Example: URI template written in plain text.

/ja/items/index.html
/en/items/index.html

Example: URI template to output Japanese and English pages, numbered with 3 digits, for each element of items (assume items is provided in source smc file).

<%- items.length.times do |i| -%>
<%= "/ja/items/#{"%03d" % i}.html" %>
<%= "/en/items/#{"%03d" % i}.html" %>
<%- end -%>

Example: URI template to output a file naming it by ‘files’ value in smc file.

<%= files.join("\n") %>

Example: URI template to output files naming them by ‘name’ property of ‘items’ Map.

<%= items.map {|item| item['name'] + ".html"}.join("\n")  %>

Example: URI template to load CSV file and output each line to a single file.

<%- require 'csv'
@obj["items"] = CSV.read(dirs[0]["path"] + "_smc/data/items.csv")
@obj["items"].each_with_index do |data, i| -%>
item-<%= data[0] %>.html
<%- end -%>

You can get these examples from gist: 228019.

Properties of smc object for URI templates

If there is a URI template, SmallCage will set special properties to smc object.

  • uris … Result of processing the URI tempalte. An array containing each line of result as one element.
  • cursor … Index number representing the position of currently processing URI in uris. 0 for the first element of uris.

‘uri’ and ‘path’ property of smc object will be overwritten by currently processing one.

Update

Pseudo-code below shows how smc update performs processing.

uris = render_uri_template
uris.each_with_index do |uri,cursor|
  next if uri.empty?
  set_uri_and_path(uri)
  render_template
end
  • Look at ‘template’ value in source smc and find a URI template named ‘(template).uri.smc’
  • If exists, process the URI template just once. The result is a list of URIs for output file.
  • Make uris array and set each line of the result to its elements.
    • uris can be used for determining links to previous/next page.
  • Walk through uris. Use each element as URI and process smc object.
    • Empty elements will be ignored.
    • set ‘uri’ and ‘path’ property of smc object.
    • ‘uri.smc’, ‘path.smc’ property will be left unchanged.
  • Set the current position on uris to cursor.
    • At the first URI, cursor is set to 0. At second, 1.
  • Perform publishing smc object as usually do.
  • Go through uris to the end, Increasing curosr number

Delete generated files

Files generated at ‘Update’ step are listed in /_smc/tmp/list.yml. On next update, SmallCage compares the list of files being generated, to the content of list.yml (files generated last time). If some files in list.yml no longer be generated in current update, SmallCage deletes them.

If a file of same name is generated from different smc file, the output will not be deleted. For example, at first a.html was generated from a.html.smc. After some modification, now a.html is generated from b.html.smc instead of a.html.smc. In this case, a.html will not be removed by smc update.

However, if filename is specified on execution like smc update a.html.smc, the result will be a little different. It determines what to delete, by referring to the files generated only from a.html.smc last time. SmallCage can’t know whether a.html will be generated from b.html.smc without processing b.html.smc.

In case you have multiple source smcs for an output file, and the source occasionally changes, then publish whole site to make things clear.

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.