A new Riff-radio.org site with a static approach.

theming.html 43KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. <dl class="docinfo simple">
  2. <dt class="version">Version<span class="colon">:</span></dt>
  3. <dd class="version">8.2.3</dd>
  4. <dt class="author">Author<span class="colon">:</span></dt>
  5. <dd class="author"><p>Roberto Alsina &lt;<a class="reference external" href="mailto:ralsina&#64;netmanagers.com.ar">ralsina&#64;netmanagers.com.ar</a>&gt;</p></dd>
  6. </dl>
  7. <nav class="contents alert alert-primary float-md-right" id="contents" role="doc-toc">
  8. <p class="topic-title">Contents</p>
  9. <ul class="simple">
  10. <li><p><a class="reference internal" href="#the-structure" id="toc-entry-1">The Structure</a></p></li>
  11. <li><p><a class="reference internal" href="#theme-meta-files" id="toc-entry-2">Theme meta files</a></p></li>
  12. <li><p><a class="reference internal" href="#templates" id="toc-entry-3">Templates</a></p></li>
  13. <li><p><a class="reference internal" href="#built-in-templates" id="toc-entry-4">Built-in templates</a></p></li>
  14. <li><p><a class="reference internal" href="#variables-available-in-templates" id="toc-entry-5">Variables available in templates</a></p></li>
  15. <li><p><a class="reference internal" href="#customizing-themes-to-user-color-preference-colorizing-category-names" id="toc-entry-6">Customizing themes to user color preference, colorizing category names</a></p></li>
  16. <li><p><a class="reference internal" href="#identifying-and-customizing-different-kinds-of-pages-with-a-shared-template" id="toc-entry-7">Identifying and customizing different kinds of pages with a shared template</a></p></li>
  17. <li><p><a class="reference internal" href="#messages-and-translations" id="toc-entry-8">Messages and Translations</a></p></li>
  18. <li><p><a class="reference internal" href="#less-and-sass" id="toc-entry-9">LESS and Sass</a></p></li>
  19. </ul>
  20. </nav>
  21. <p class="lead">This document is a reference about themes. If you want a tutorial, please read
  22. <a class="reference external" href="/pages/creating-a-theme.html">Creating a Theme</a>. If you’re looking for a ready-made
  23. theme for your site, check out the <a class="reference external" href="https://themes.getnikola.com/">Themes Index</a>.</p>
  24. <section id="the-structure">
  25. <h1><a class="toc-backref" href="#toc-entry-1" role="doc-backlink">The Structure</a></h1>
  26. <p>Themes are located in the <code class="docutils literal">themes</code> folder where Nikola is installed, and in the <code class="docutils literal">themes</code> folder
  27. of your site, one folder per theme. The folder name is the theme name.</p>
  28. <p>A Nikola theme consists of the following folders (they are <em>all</em> optional):</p>
  29. <dl>
  30. <dt>assets</dt>
  31. <dd><p>This is where you would put your CSS, JavaScript and image files. It will be copied
  32. into <code class="docutils literal">output/assets</code> when you build the site, and the templates will contain
  33. references to them. The default subdirectories are <code class="docutils literal">css</code>, <code class="docutils literal">js</code>, <code class="docutils literal">xml</code>
  34. and <code class="docutils literal">fonts</code> (Bootstrap).</p>
  35. <p>The included themes use <a class="reference external" href="https://getbootstrap.com/">Bootstrap</a>,
  36. <a class="reference external" href="https://feimosi.github.io/baguetteBox.js/">baguetteBox</a>, <a class="reference external" href="https://flickr.github.io/justified-layout/">Justified Layout by Flickr</a> and <a class="reference external" href="https://moment.github.io/luxon/">Luxon</a>, so they are in assets, along with CSS files for
  37. syntax highlighting, reStructuredText and Jupyter, as well as a minified
  38. copy of jQuery.</p>
  39. <p>If you want to base your theme on other frameworks (or on no framework at all)
  40. just remember to put there everything you need for deployment. (Not all of
  41. the listed assets are used by <code class="docutils literal">base</code>)</p>
  42. </dd>
  43. <dt>templates</dt>
  44. <dd><p>This contains the templates used to generate the pages. While Nikola will use a
  45. certain set of template names by default, you can add others for specific parts
  46. of your site.</p>
  47. </dd>
  48. <dt>messages</dt>
  49. <dd><p>Nikola tries to be multilingual. This is where you put the strings for your theme
  50. so that it can be translated into other languages.</p>
  51. </dd>
  52. <dt>less, sass</dt>
  53. <dd><p>Files to be compiled into CSS using LESS and Sass (both require plugins)</p>
  54. </dd>
  55. </dl>
  56. <p>This mandatory file:</p>
  57. <dl class="simple">
  58. <dt>&lt;theme&gt;.theme</dt>
  59. <dd><p>An INI file containing theme meta data. The file format is described in
  60. detail below, in the <a class="reference internal" href="#theme-meta-files">Theme meta files</a> section.</p>
  61. </dd>
  62. </dl>
  63. <p>And these optional files:</p>
  64. <dl>
  65. <dt>parent, engine</dt>
  66. <dd><p>One-line text files that contain the names of parent and engine themes,
  67. respectively. Those are needed for older versions (Nikola v7.8.5 and
  68. older).</p>
  69. </dd>
  70. <dt>bundles</dt>
  71. <dd><p>A <a class="reference external" href="https://docs.python.org/3/library/configparser.html">config</a> file
  72. containing a list of files to be turned into bundles. For example:</p>
  73. <pre class="code ini"><a name="rest_code_1e62c0f8635c468a9e9e465b2eb0939e-1"></a><span class="na">assets/css/all.css</span><span class="o">=</span><span class="s"></span>
  74. <a name="rest_code_1e62c0f8635c468a9e9e465b2eb0939e-2"></a><span class="s"> bootstrap.min.css,</span>
  75. <a name="rest_code_1e62c0f8635c468a9e9e465b2eb0939e-3"></a><span class="s"> rst_base.css,</span>
  76. <a name="rest_code_1e62c0f8635c468a9e9e465b2eb0939e-4"></a><span class="s"> nikola_rst.css,</span>
  77. <a name="rest_code_1e62c0f8635c468a9e9e465b2eb0939e-5"></a><span class="s"> code.css,</span>
  78. <a name="rest_code_1e62c0f8635c468a9e9e465b2eb0939e-6"></a><span class="s"> baguetteBox.min.css,</span>
  79. <a name="rest_code_1e62c0f8635c468a9e9e465b2eb0939e-7"></a><span class="s"> theme.css,</span>
  80. <a name="rest_code_1e62c0f8635c468a9e9e465b2eb0939e-8"></a><span class="s"> custom.css,</span>
  81. </pre><p>This creates a file called &quot;assets/css/all.css&quot; in your output that is the
  82. combination of all the other file paths, relative to the output file.
  83. This makes the page much more efficient because it avoids multiple connections to the server,
  84. at the cost of some extra difficult debugging.</p>
  85. <p>Bundling applies to CSS and JS files.</p>
  86. <p>Templates should use either the bundle or the individual files based on the <code class="docutils literal">use_bundles</code>
  87. variable, which in turn is set by the <code class="docutils literal">USE_BUNDLES</code> option.</p>
  88. </dd>
  89. </dl>
  90. </section>
  91. <section id="theme-meta-files">
  92. <h1><a class="toc-backref" href="#toc-entry-2" role="doc-backlink">Theme meta files</a></h1>
  93. <p>As of Nikola v7.8.6, Nikola uses meta files for themes. Those are INI files,
  94. with the same name as your theme, and a <code class="docutils literal">.theme</code> extension, eg.
  95. <code class="docutils literal">bootstrap3.theme</code>. Here is an example, from the bootstrap3 theme:</p>
  96. <pre class="code ini"><a name="rest_code_929f518a59e14434bd6c9df89e29e59d-1"></a><span class="k">[Theme]</span>
  97. <a name="rest_code_929f518a59e14434bd6c9df89e29e59d-2"></a><span class="na">engine</span> <span class="o">=</span> <span class="s">mako</span>
  98. <a name="rest_code_929f518a59e14434bd6c9df89e29e59d-3"></a><span class="na">parent</span> <span class="o">=</span> <span class="s">base</span>
  99. <a name="rest_code_929f518a59e14434bd6c9df89e29e59d-4"></a><span class="na">author</span> <span class="o">=</span> <span class="s">The Nikola Contributors</span>
  100. <a name="rest_code_929f518a59e14434bd6c9df89e29e59d-5"></a><span class="na">author_url</span> <span class="o">=</span> <span class="s">https://getnikola.com/</span>
  101. <a name="rest_code_929f518a59e14434bd6c9df89e29e59d-6"></a><span class="na">based_on</span> <span class="o">=</span> <span class="s">Bootstrap 3 &lt;https://getbootstrap.com/&gt;</span>
  102. <a name="rest_code_929f518a59e14434bd6c9df89e29e59d-7"></a><span class="na">license</span> <span class="o">=</span> <span class="s">MIT</span>
  103. <a name="rest_code_929f518a59e14434bd6c9df89e29e59d-8"></a><span class="na">tags</span> <span class="o">=</span> <span class="s">bootstrap</span>
  104. <a name="rest_code_929f518a59e14434bd6c9df89e29e59d-9"></a>
  105. <a name="rest_code_929f518a59e14434bd6c9df89e29e59d-10"></a><span class="k">[Family]</span>
  106. <a name="rest_code_929f518a59e14434bd6c9df89e29e59d-11"></a><span class="na">family</span> <span class="o">=</span> <span class="s">bootstrap3</span>
  107. <a name="rest_code_929f518a59e14434bd6c9df89e29e59d-12"></a><span class="na">jinja_version</span> <span class="o">=</span> <span class="s">bootstrap3-jinja</span>
  108. <a name="rest_code_929f518a59e14434bd6c9df89e29e59d-13"></a><span class="na">variants</span> <span class="o">=</span> <span class="s">bootstrap3-gradients, bootstrap3-gradients-jinja</span>
  109. <a name="rest_code_929f518a59e14434bd6c9df89e29e59d-14"></a>
  110. <a name="rest_code_929f518a59e14434bd6c9df89e29e59d-15"></a><span class="k">[Nikola]</span>
  111. <a name="rest_code_929f518a59e14434bd6c9df89e29e59d-16"></a><span class="na">bootswatch</span> <span class="o">=</span> <span class="s">True</span>
  112. </pre><p>The following keys are currently supported:</p>
  113. <ul>
  114. <li><p><code class="docutils literal">Theme</code> — contains information about the theme.</p>
  115. <ul>
  116. <li><p><code class="docutils literal">engine</code> — engine used by the theme. Should be <code class="docutils literal">mako</code> or <code class="docutils literal">jinja</code>.</p></li>
  117. <li><p><code class="docutils literal">parent</code> — the parent theme. Any resources missing in this theme, will be
  118. looked up in the parent theme (and then in the grandparent, etc).</p>
  119. <p>The parent is so you don’t have to create a full theme each time: just
  120. create an empty theme, set the parent, and add the bits you want modified.
  121. You <strong>must</strong> define a parent, otherwise many features won’t work due to
  122. missing templates, messages, and assets.</p>
  123. <p>The following settings are recommended:</p>
  124. <ul class="simple">
  125. <li><p>If your theme uses Bootstrap 3, inherit the <code class="docutils literal">bootstrap3</code> theme.</p></li>
  126. <li><p>If your theme uses Jinja as a template engine, inherit <code class="docutils literal"><span class="pre">base-jinja</span></code>
  127. or <code class="docutils literal"><span class="pre">bootstrap3-jinja</span></code></p></li>
  128. <li><p>In any other case, inherit <code class="docutils literal">base</code>.</p></li>
  129. </ul>
  130. </li>
  131. <li><p><code class="docutils literal">author</code>, <code class="docutils literal">author_url</code> — used to identify theme author.</p></li>
  132. <li><p><code class="docutils literal">based_on</code> — optional list of inspirations, frameworks, etc. used in the
  133. theme. Should be comma-separated, the format <code class="docutils literal">Name &lt;URL&gt;</code> is recommended.</p></li>
  134. <li><p><code class="docutils literal">license</code> — theme license. Pick MIT if you have no preference.</p></li>
  135. <li><p><code class="docutils literal">tags</code> — optional list of tags (comma-separated) to describe the theme.</p></li>
  136. </ul>
  137. </li>
  138. <li><p><code class="docutils literal">Family</code> — contains information about other related themes. All values
  139. optional. (Do not use unless you have related themes.)</p>
  140. <ul class="simple">
  141. <li><p><code class="docutils literal">family</code> — the name of the main theme in a family, which is also used as
  142. the family name.</p></li>
  143. <li><p><code class="docutils literal">mako_version</code>, <code class="docutils literal">jinja_version</code> — name of the mako/jinja version of the
  144. theme.</p></li>
  145. <li><p><code class="docutils literal">variants</code> — comma-separated list of stylistic variants (other than the
  146. mako/jinja version listed above)</p></li>
  147. </ul>
  148. </li>
  149. <li><p><code class="docutils literal">Nikola</code> — Nikola-specific information, currently optional.</p>
  150. <ul class="simple">
  151. <li><p><code class="docutils literal">bootswatch</code> — whether or not theme supports Bootswatch styling (optional,
  152. defaults to False)</p></li>
  153. <li><p><code class="docutils literal">ignored_assets</code> — comma-separated list of assets to ignore (relative to
  154. the <code class="docutils literal">assets/</code> directory, eg. <code class="docutils literal">css/theme.css</code>)</p></li>
  155. </ul>
  156. </li>
  157. </ul>
  158. </section>
  159. <section id="templates">
  160. <h1><a class="toc-backref" href="#toc-entry-3" role="doc-backlink">Templates</a></h1>
  161. <p>In templates there is a number of files whose name ends in <code class="docutils literal">.tmpl</code>. Those are the
  162. theme’s page templates. They are done using the <a class="reference external" href="https://www.makotemplates.org">Mako</a>
  163. or <a class="reference external" href="http://jinja.pocoo.org">Jinja2</a> template languages. If you want to do a theme, you
  164. should learn one first. What engine is used by the theme is declared in the <code class="docutils literal">engine</code> file.</p>
  165. <aside class="admonition tip">
  166. <p class="admonition-title">Tip</p>
  167. <p>If you are using Mako templates, and want some extra speed when building the site
  168. you can install Beaker and <a class="reference external" href="https://docs.makotemplates.org/en/latest/caching.html">make templates be cached</a></p>
  169. </aside>
  170. <p>Both template engines have a nifty concept of template inheritance. That means that a
  171. template can inherit from another and only change small bits of the output. For example,
  172. <code class="docutils literal">base.tmpl</code> defines the whole layout for a page but has only a placeholder for content
  173. so <code class="docutils literal">post.tmpl</code> only define the content, and the layout is inherited from <code class="docutils literal">base.tmpl</code>.</p>
  174. <p>Another concept is theme inheritance. You do not need to duplicate all the
  175. default templates in your theme — you can just override the ones you want
  176. changed, and the rest will come from the parent theme. (Every theme needs a
  177. parent.)</p>
  178. <p>Apart from the <a class="reference internal" href="#built-in-templates">built-in templates</a> listed below, you can add other templates for specific
  179. pages, which the user can then use in his <code class="docutils literal">POSTS</code> or <code class="docutils literal">PAGES</code> option in
  180. <code class="docutils literal">conf.py</code>. Also, you can specify a custom template to be used by a post or
  181. page via the <code class="docutils literal">template</code> metadata, and custom templates can be added in the
  182. <code class="docutils literal">templates/</code> folder of your site.</p>
  183. <p>If you want to modify (override) a built-in template, use <code class="docutils literal">nikola theme <span class="pre">-c</span>
  184. <span class="pre">&lt;name&gt;.tmpl</span></code>. This command will copy the specified template file to the
  185. <code class="docutils literal">templates/</code> directory of your currently used theme.</p>
  186. <p>Keep in mind that your theme is <em>yours</em>, so you can require whatever data you
  187. want (eg. you may depend on specific custom <code class="docutils literal">GLOBAL_CONTEXT</code> variables, or
  188. post meta attributes). You don’t need to keep the same theme structure as the
  189. default themes do (although many of those names are hardcoded). Inheriting from
  190. at least <code class="docutils literal">base</code> (or <code class="docutils literal"><span class="pre">base-jinja</span></code>) is heavily recommended, but not strictly
  191. required (unless you want to share it on the Themes Index).</p>
  192. </section>
  193. <section id="built-in-templates">
  194. <h1><a class="toc-backref" href="#toc-entry-4" role="doc-backlink">Built-in templates</a></h1>
  195. <p>These are the templates that come with the included themes:</p>
  196. <dl>
  197. <dt><code class="docutils literal">base.tmpl</code></dt>
  198. <dd><p>This template defines the basic page layout for the site. It’s mostly plain HTML
  199. but defines a few blocks that can be re-defined by inheriting templates.</p>
  200. <p>It has some separate pieces defined in <code class="docutils literal">base_helper.tmpl</code>,
  201. <code class="docutils literal">base_header.tmpl</code> and <code class="docutils literal">base_footer.tmpl</code> so they can be
  202. easily overridden.</p>
  203. </dd>
  204. <dt><code class="docutils literal">index.tmpl</code></dt>
  205. <dd><p>Template used to render the multipost indexes. The posts are in a <code class="docutils literal">posts</code> variable.
  206. Some functionality is in the <code class="docutils literal">index_helper.tmpl</code> helper template.</p>
  207. </dd>
  208. <dt><code class="docutils literal">archive_navigation_helper.tmpl</code> (internal)</dt>
  209. <dd><p>Code that implements archive navigation (previous/up/next). Included by
  210. archive templates.</p>
  211. </dd>
  212. <dt><code class="docutils literal">archiveindex.tmpl</code></dt>
  213. <dd><p>Used to display archives, if <code class="docutils literal">ARCHIVES_ARE_INDEXES</code> is True.
  214. By default, it just inherits <code class="docutils literal">index.tmpl</code>, with added archive navigation
  215. and feeds.</p>
  216. </dd>
  217. <dt><code class="docutils literal">author.tmpl</code></dt>
  218. <dd><p>Used to display author pages.</p>
  219. </dd>
  220. <dt><code class="docutils literal">authorindex.tmpl</code></dt>
  221. <dd><p>Used to display author indexes, if <code class="docutils literal">AUTHOR_PAGES_ARE_INDEXES</code> is True.
  222. By default, it just inherits <code class="docutils literal">index.tmpl</code>, with added feeds.</p>
  223. </dd>
  224. <dt><code class="docutils literal">comments_helper.tmpl</code> (internal)</dt>
  225. <dd><p>This template handles comments. You should probably never touch it :-)
  226. It uses a bunch of helper templates, one for each supported comment system
  227. (all of which start with <code class="docutils literal">comments_helper</code>)</p>
  228. </dd>
  229. <dt><code class="docutils literal">ui_helper.tmpl</code>, <code class="docutils literal">pagination_helper.tmpl</code></dt>
  230. <dd><p>These templates help render specific UI items, and can be tweaked as needed.</p>
  231. </dd>
  232. <dt><code class="docutils literal">gallery.tmpl</code></dt>
  233. <dd><p>Template used for image galleries. Interesting data includes:</p>
  234. <ul class="simple">
  235. <li><p><code class="docutils literal">post</code>: A post object, containing descriptive <code class="docutils literal">post.text()</code> for the gallery.</p></li>
  236. <li><p><code class="docutils literal">crumbs</code>: A list of <code class="docutils literal">link, crumb</code> to implement breadcrumbs.</p></li>
  237. <li><p><code class="docutils literal">folders</code>: A list of folders to implement hierarchical gallery navigation.</p></li>
  238. <li><p><code class="docutils literal">enable_comments</code>: To enable/disable comments in galleries.</p></li>
  239. <li><p><code class="docutils literal">thumbnail_size</code>: The <code class="docutils literal">THUMBNAIL_SIZE</code> option.</p></li>
  240. <li><p><code class="docutils literal">photo_array</code>: a list of dictionaries, each containing:</p>
  241. <ul>
  242. <li><p><code class="docutils literal">url</code>: URL for the full-sized image.</p></li>
  243. <li><p><code class="docutils literal">url_thumb</code>: URL for the thumbnail.</p></li>
  244. <li><p><code class="docutils literal">title</code>: The title of the image.</p></li>
  245. <li><p><code class="docutils literal">size</code>: A dict containing <code class="docutils literal">w</code> and <code class="docutils literal">h</code>, the real size of the thumbnail.</p></li>
  246. </ul>
  247. </li>
  248. <li><p><code class="docutils literal">photo_array_json</code>: a JSON dump of photo_array, used by the
  249. <code class="docutils literal"><span class="pre">justified-layout</span></code> script</p></li>
  250. </ul>
  251. </dd>
  252. <dt><code class="docutils literal">list.tmpl</code></dt>
  253. <dd><p>Template used to display generic lists of links, which it gets in <code class="docutils literal">items</code>,
  254. a list of <em>(text, link, count)</em> elements.</p>
  255. </dd>
  256. <dt><code class="docutils literal">list_post.tmpl</code></dt>
  257. <dd><p>Template used to display generic lists of posts, which it gets in <code class="docutils literal">posts</code>.</p>
  258. </dd>
  259. <dt><code class="docutils literal">listing.tmpl</code></dt>
  260. <dd><p>Used to display code listings.</p>
  261. </dd>
  262. <dt><code class="docutils literal">math_helper.tmpl</code> (internal)</dt>
  263. <dd><p>Used to add MathJax/KaTeX code to pages.</p>
  264. </dd>
  265. <dt><code class="docutils literal">post.tmpl</code></dt>
  266. <dd><p>Template used by default for blog posts, gets the data in a <code class="docutils literal">post</code> object
  267. which is an instance of the Post class. Some functionality is in the
  268. <code class="docutils literal">post_helper.tmpl</code> and <code class="docutils literal">post_header.tmpl</code> templates.</p>
  269. </dd>
  270. <dt><code class="docutils literal">post_list_directive.tmpl</code></dt>
  271. <dd><p>Template used by the <code class="docutils literal">post_list</code> reStructuredText directive.</p>
  272. </dd>
  273. <dt><code class="docutils literal">page.tmpl</code></dt>
  274. <dd><p>Used for pages that are not part of a blog, usually a cleaner, less
  275. intrusive layout than <code class="docutils literal">post.tmpl</code>, but same parameters.</p>
  276. </dd>
  277. <dt><code class="docutils literal">tag.tmpl</code></dt>
  278. <dd><p>Used to show the contents of a single tag or category.</p>
  279. </dd>
  280. <dt><code class="docutils literal">tagindex.tmpl</code></dt>
  281. <dd><p>Used to show the contents of a single tag or category, if <code class="docutils literal">TAG_PAGES_ARE_INDEXES</code> is True.
  282. By default, it just inherits <code class="docutils literal">index.tmpl</code>, with added feeds and some
  283. extra features.</p>
  284. </dd>
  285. <dt><code class="docutils literal">tags.tmpl</code></dt>
  286. <dd><p>Used to display the list of tags and categories.</p>
  287. </dd>
  288. </dl>
  289. </section>
  290. <section id="variables-available-in-templates">
  291. <h1><a class="toc-backref" href="#toc-entry-5" role="doc-backlink">Variables available in templates</a></h1>
  292. <p>The full, complete list of variables available in templates is maintained in a separate
  293. document: <a class="reference external" href="https://getnikola.com/template-variables.html">Template variables</a></p>
  294. </section>
  295. <section id="customizing-themes-to-user-color-preference-colorizing-category-names">
  296. <h1><a class="toc-backref" href="#toc-entry-6" role="doc-backlink">Customizing themes to user color preference, colorizing category names</a></h1>
  297. <p>The user’s preference for theme color is exposed in templates as
  298. <code class="docutils literal">theme_color</code> set in the <code class="docutils literal">THEME_COLOR</code> option.</p>
  299. <p>This theme color is exposed to the browser in default themes — some browsers
  300. might use this color in the user interface (eg. Chrome on Android in light mode
  301. displays the toolbar in this color).</p>
  302. <p>Nikola also comes with support for auto-generating colors similar to a base
  303. color. This can be used with <code class="docutils literal">theme_color</code> and eg. category names. This
  304. feature is exposed to templates as two functions: <code class="docutils literal">colorize_str(string,
  305. hex_color, presets)</code> and <code class="docutils literal">colorize_str_from_base_color(string, hex_color)</code>.
  306. If you want to display the category name in the color, first define a list of
  307. overrides in your <code class="docutils literal">conf.py</code> file:</p>
  308. <pre class="code python"><a name="rest_code_e1e6e214a9724e84bd4409ca879f1410-1"></a><span class="c1"># end of conf.py</span>
  309. <a name="rest_code_e1e6e214a9724e84bd4409ca879f1410-2"></a><span class="n">GLOBAL_CONTEXT</span> <span class="o">=</span> <span class="p">{</span>
  310. <a name="rest_code_e1e6e214a9724e84bd4409ca879f1410-3"></a> <span class="s2">&quot;category_colors&quot;</span><span class="p">:</span> <span class="p">{</span>
  311. <a name="rest_code_e1e6e214a9724e84bd4409ca879f1410-4"></a> <span class="s2">&quot;Blue&quot;</span><span class="p">:</span> <span class="s2">&quot;#0000FF&quot;</span>
  312. <a name="rest_code_e1e6e214a9724e84bd4409ca879f1410-5"></a> <span class="p">}</span>
  313. <a name="rest_code_e1e6e214a9724e84bd4409ca879f1410-6"></a><span class="p">}</span>
  314. </pre><p>With that definition, you can now use <code class="docutils literal">colorize_str</code> in your templates like this:</p>
  315. <pre class="code html+mako"><a name="rest_code_fba7b12c95374a1cbb5c15eb884d099c-1"></a><span class="c">&lt;!-- Mako --&gt;</span>
  316. <a name="rest_code_fba7b12c95374a1cbb5c15eb884d099c-2"></a><span class="p">&lt;</span><span class="nt">span</span> <span class="na">style</span><span class="o">=</span><span class="s">&quot;background-color: </span><span class="cp">${</span><span class="n">colorize_str</span><span class="p">(</span><span class="n">post</span><span class="o">.</span><span class="n">meta</span><span class="p">(</span><span class="s1">&#39;category&#39;</span><span class="p">),</span> <span class="n">theme_color</span><span class="p">,</span> <span class="n">category_colors</span><span class="p">)</span><span class="cp">}</span><span class="s">&quot;</span><span class="p">&gt;</span><span class="cp">${</span><span class="n">post</span><span class="o">.</span><span class="n">meta</span><span class="p">(</span><span class="s1">&#39;category&#39;</span><span class="p">)</span><span class="cp">}</span><span class="p">&lt;/</span><span class="nt">span</span><span class="p">&gt;</span>
  317. </pre><pre class="code html+jinja"><a name="rest_code_61d83b94de6c4f51b3b59b3a4baf9b50-1"></a><span class="c">&lt;!-- Jinja2 --&gt;</span>
  318. <a name="rest_code_61d83b94de6c4f51b3b59b3a4baf9b50-2"></a><span class="p">&lt;</span><span class="nt">span</span> <span class="na">style</span><span class="o">=</span><span class="s">&quot;background-color: </span><span class="cp">{{</span> <span class="nv">colorize_str</span><span class="o">(</span><span class="nv">post.meta</span><span class="o">(</span><span class="s1">&#39;category&#39;</span><span class="o">),</span> <span class="nv">theme_color</span><span class="o">,</span> <span class="nv">category_colors</span><span class="o">)</span> <span class="cp">}}</span><span class="s">&quot;</span><span class="p">&gt;</span><span class="cp">{{</span> <span class="nv">post.meta</span><span class="o">(</span><span class="s1">&#39;category&#39;</span><span class="o">)</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">span</span><span class="p">&gt;</span>
  319. </pre><p>Note that the category named “Blue” will be displyed in #0000FF due to the
  320. override specified in your config; other categories will have an auto-generated
  321. color visually similar to your theme color.</p>
  322. <p>Hex color values, like that returned by the theme or string colorization can be
  323. altered in the HSL colorspace through the function
  324. <code class="docutils literal">color_hsl_adjust_hex(hex_string, adjust_h, adjust_s, adjust_l)</code>.
  325. Adjustments are given in values between 1.0 and -1.0. For example, the theme
  326. color can be made lighter using this code:</p>
  327. <pre class="code html+mako"><a name="rest_code_09d4261e8af8487ab07c92c3b2b9d794-1"></a><span class="c">&lt;!-- Mako --&gt;</span>
  328. <a name="rest_code_09d4261e8af8487ab07c92c3b2b9d794-2"></a><span class="p">&lt;</span><span class="nt">span</span> <span class="na">style</span><span class="o">=</span><span class="s">&quot;color: </span><span class="cp">${</span><span class="n">color_hsl_adjust_hex</span><span class="p">(</span><span class="n">theme_color</span><span class="p">,</span> <span class="n">adjust_l</span><span class="o">=</span><span class="mf">0.05</span><span class="p">)</span><span class="cp">}</span><span class="s">&quot;</span><span class="p">&gt;</span>
  329. </pre><pre class="code html+jinja"><a name="rest_code_11fca1d7d4a942dd8dce2c8624ac1e9c-1"></a><span class="c">&lt;!-- Jinja2 --&gt;</span>
  330. <a name="rest_code_11fca1d7d4a942dd8dce2c8624ac1e9c-2"></a><span class="p">&lt;</span><span class="nt">span</span> <span class="na">style</span><span class="o">=</span><span class="s">&quot;color: </span><span class="cp">{{</span> <span class="nv">color_hsl_adjust_hex</span><span class="o">(</span><span class="nv">theme_color</span><span class="o">,</span> <span class="nv">adjust_l</span><span class="o">=</span><span class="m">0.05</span><span class="o">)</span> <span class="cp">}}</span><span class="s">&quot;</span><span class="p">&gt;</span>
  331. </pre></section>
  332. <section id="identifying-and-customizing-different-kinds-of-pages-with-a-shared-template">
  333. <h1><a class="toc-backref" href="#toc-entry-7" role="doc-backlink">Identifying and customizing different kinds of pages with a shared template</a></h1>
  334. <p>Nikola provides a <code class="docutils literal">pagekind</code> in each template contexts that can be used to
  335. modify shared templates based on the context it’s being used. For example,
  336. the <code class="docutils literal">base_helper.tmpl</code> is used in all pages, <code class="docutils literal">index.tmpl</code> is used in
  337. many contexts and you may want to add or remove something from only one of
  338. these contexts.</p>
  339. <p>Example of conditionally loading different resources on all index pages
  340. (archives, author pages, and tag pages), and others again to the front page
  341. and in every post pages:</p>
  342. <pre class="code html+mako"><a name="rest_code_8477a412a8d044dfa96ede312ef0d4d8-1"></a><span class="c">&lt;!-- Mako --&gt;</span>
  343. <a name="rest_code_8477a412a8d044dfa96ede312ef0d4d8-2"></a><span class="p">&lt;</span><span class="nt">head</span><span class="p">&gt;</span>
  344. <a name="rest_code_8477a412a8d044dfa96ede312ef0d4d8-3"></a> …
  345. <a name="rest_code_8477a412a8d044dfa96ede312ef0d4d8-4"></a> <span class="cp">%</span> <span class="k">if</span> <span class="s1">&#39;index&#39;</span> <span class="ow">in</span> <span class="n">pagekind</span><span class="p">:</span>
  346. <a name="rest_code_8477a412a8d044dfa96ede312ef0d4d8-5"></a> <span class="p">&lt;</span><span class="nt">link</span> <span class="na">href</span><span class="o">=</span><span class="s">&quot;/assets/css/multicolumn.css&quot;</span> <span class="na">rel</span><span class="o">=</span><span class="s">&quot;stylesheet&quot;</span><span class="p">&gt;</span>
  347. <a name="rest_code_8477a412a8d044dfa96ede312ef0d4d8-6"></a> <span class="cp">%</span><span class="k"> endif</span>
  348. <a name="rest_code_8477a412a8d044dfa96ede312ef0d4d8-7"></a> <span class="cp">%</span> <span class="k">if</span> <span class="s1">&#39;front_page&#39;</span> <span class="ow">in</span> <span class="n">pagekind</span><span class="p">:</span>
  349. <a name="rest_code_8477a412a8d044dfa96ede312ef0d4d8-8"></a> <span class="p">&lt;</span><span class="nt">link</span> <span class="na">href</span><span class="o">=</span><span class="s">&quot;/assets/css/fancy_homepage.css&quot;</span> <span class="na">rel</span><span class="o">=</span><span class="s">&quot;stylesheet&quot;</span><span class="p">&gt;</span>
  350. <a name="rest_code_8477a412a8d044dfa96ede312ef0d4d8-9"></a> <span class="p">&lt;</span><span class="nt">script</span> <span class="na">src</span><span class="o">=</span><span class="s">&quot;/assets/js/post_carousel.js&quot;</span><span class="p">&gt;&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>
  351. <a name="rest_code_8477a412a8d044dfa96ede312ef0d4d8-10"></a> <span class="cp">%</span><span class="k"> endif</span>
  352. <a name="rest_code_8477a412a8d044dfa96ede312ef0d4d8-11"></a> <span class="cp">%</span> <span class="k">if</span> <span class="s1">&#39;post_page&#39;</span> <span class="ow">in</span> <span class="n">pagekind</span><span class="p">:</span>
  353. <a name="rest_code_8477a412a8d044dfa96ede312ef0d4d8-12"></a> <span class="p">&lt;</span><span class="nt">link</span> <span class="na">href</span><span class="o">=</span><span class="s">&quot;/assets/css/article.css&quot;</span> <span class="na">rel</span><span class="o">=</span><span class="s">&quot;stylesheet&quot;</span><span class="p">&gt;</span>
  354. <a name="rest_code_8477a412a8d044dfa96ede312ef0d4d8-13"></a> <span class="p">&lt;</span><span class="nt">script</span> <span class="na">src</span><span class="o">=</span><span class="s">&quot;/assets/js/comment_system.js&quot;</span><span class="p">&gt;&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>
  355. <a name="rest_code_8477a412a8d044dfa96ede312ef0d4d8-14"></a> <span class="cp">%</span><span class="k"> endif</span>
  356. <a name="rest_code_8477a412a8d044dfa96ede312ef0d4d8-15"></a><span class="p">&lt;/</span><span class="nt">head</span><span class="p">&gt;</span>
  357. </pre><pre class="code html+jinja"><a name="rest_code_c83f454f264a4909ac8903c54f97d97d-1"></a><span class="c">&lt;!-- Jinja2 --&gt;</span>
  358. <a name="rest_code_c83f454f264a4909ac8903c54f97d97d-2"></a><span class="p">&lt;</span><span class="nt">head</span><span class="p">&gt;</span>
  359. <a name="rest_code_c83f454f264a4909ac8903c54f97d97d-3"></a> …
  360. <a name="rest_code_c83f454f264a4909ac8903c54f97d97d-4"></a> <span class="cp">{%</span> <span class="k">if</span> <span class="s1">&#39;index&#39;</span> <span class="k">in</span> <span class="nv">pagekind</span> <span class="cp">%}</span>
  361. <a name="rest_code_c83f454f264a4909ac8903c54f97d97d-5"></a> <span class="p">&lt;</span><span class="nt">link</span> <span class="na">href</span><span class="o">=</span><span class="s">&quot;/assets/css/multicolumn.css&quot;</span> <span class="na">rel</span><span class="o">=</span><span class="s">&quot;stylesheet&quot;</span><span class="p">&gt;</span>
  362. <a name="rest_code_c83f454f264a4909ac8903c54f97d97d-6"></a> <span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
  363. <a name="rest_code_c83f454f264a4909ac8903c54f97d97d-7"></a> <span class="cp">{%</span> <span class="k">if</span> <span class="s1">&#39;front_page&#39;</span> <span class="k">in</span> <span class="nv">pagekind</span> <span class="cp">%}</span>
  364. <a name="rest_code_c83f454f264a4909ac8903c54f97d97d-8"></a> <span class="p">&lt;</span><span class="nt">link</span> <span class="na">href</span><span class="o">=</span><span class="s">&quot;/assets/css/fancy_homepage.css&quot;</span> <span class="na">rel</span><span class="o">=</span><span class="s">&quot;stylesheet&quot;</span><span class="p">&gt;</span>
  365. <a name="rest_code_c83f454f264a4909ac8903c54f97d97d-9"></a> <span class="p">&lt;</span><span class="nt">script</span> <span class="na">src</span><span class="o">=</span><span class="s">&quot;/assets/js/post_carousel.js&quot;</span><span class="p">&gt;&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>
  366. <a name="rest_code_c83f454f264a4909ac8903c54f97d97d-10"></a> <span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
  367. <a name="rest_code_c83f454f264a4909ac8903c54f97d97d-11"></a> <span class="cp">{%</span> <span class="k">if</span> <span class="s1">&#39;post_page&#39;</span> <span class="k">in</span> <span class="nv">pagekind</span> <span class="cp">%}</span>
  368. <a name="rest_code_c83f454f264a4909ac8903c54f97d97d-12"></a> <span class="p">&lt;</span><span class="nt">link</span> <span class="na">href</span><span class="o">=</span><span class="s">&quot;/assets/css/article.css&quot;</span> <span class="na">rel</span><span class="o">=</span><span class="s">&quot;stylesheet&quot;</span><span class="p">&gt;</span>
  369. <a name="rest_code_c83f454f264a4909ac8903c54f97d97d-13"></a> <span class="p">&lt;</span><span class="nt">script</span> <span class="na">src</span><span class="o">=</span><span class="s">&quot;/assets/js/comment_system.js&quot;</span><span class="p">&gt;&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>
  370. <a name="rest_code_c83f454f264a4909ac8903c54f97d97d-14"></a> <span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
  371. <a name="rest_code_c83f454f264a4909ac8903c54f97d97d-15"></a><span class="p">&lt;/</span><span class="nt">head</span><span class="p">&gt;</span>
  372. </pre><p>Promoting visits to the front page when visiting other filtered
  373. <code class="docutils literal">index.tmpl</code> page variants such as author pages and tag pages. This
  374. could have been included in <code class="docutils literal">index.tmpl</code> or maybe in <code class="docutils literal">base.tmpl</code>
  375. depending on what you want to achieve.</p>
  376. <pre class="code html+mako"><a name="rest_code_c1bfafa3138941eab0bf9d2de02c820d-1"></a><span class="c">&lt;!-- Mako --&gt;</span>
  377. <a name="rest_code_c1bfafa3138941eab0bf9d2de02c820d-2"></a><span class="cp">%</span> <span class="k">if</span> <span class="s1">&#39;index&#39;</span> <span class="ow">in</span> <span class="n">pagekind</span><span class="p">:</span>
  378. <a name="rest_code_c1bfafa3138941eab0bf9d2de02c820d-3"></a> <span class="cp">%</span> <span class="k">if</span> <span class="s1">&#39;author_page&#39;</span> <span class="ow">in</span> <span class="n">pagekind</span><span class="p">:</span>
  379. <a name="rest_code_c1bfafa3138941eab0bf9d2de02c820d-4"></a> <span class="p">&lt;</span><span class="nt">p</span><span class="p">&gt;</span>These posts were written by <span class="cp">${</span><span class="n">author</span><span class="cp">}</span>. See posts by all
  380. <a name="rest_code_c1bfafa3138941eab0bf9d2de02c820d-5"></a> authors on the <span class="p">&lt;</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">&quot;/&quot;</span><span class="p">&gt;</span>front page<span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;</span>.<span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
  381. <a name="rest_code_c1bfafa3138941eab0bf9d2de02c820d-6"></a> <span class="cp">%</span> <span class="k">elif</span> <span class="s1">&#39;tag_page&#39;</span> <span class="ow">in</span> <span class="n">pagekind</span><span class="p">:</span>
  382. <a name="rest_code_c1bfafa3138941eab0bf9d2de02c820d-7"></a> <span class="p">&lt;</span><span class="nt">p</span><span class="p">&gt;</span>This is a filtered selection of posts tagged “<span class="cp">${</span><span class="n">tag</span><span class="cp">}</span>”, visit
  383. <a name="rest_code_c1bfafa3138941eab0bf9d2de02c820d-8"></a> the <span class="p">&lt;</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">&quot;/&quot;</span><span class="p">&gt;</span>front page<span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;</span> to see all posts.<span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
  384. <a name="rest_code_c1bfafa3138941eab0bf9d2de02c820d-9"></a> <span class="cp">%</span><span class="k"> endif</span>
  385. <a name="rest_code_c1bfafa3138941eab0bf9d2de02c820d-10"></a><span class="cp">%</span><span class="k"> endif</span>
  386. </pre><pre class="code html+jinja"><a name="rest_code_611dcf30e7d4429a95ab119bf2981fdd-1"></a><span class="c">&lt;!-- Jinja2 --&gt;</span>
  387. <a name="rest_code_611dcf30e7d4429a95ab119bf2981fdd-2"></a><span class="cp">{%</span> <span class="k">if</span> <span class="s1">&#39;index&#39;</span> <span class="k">in</span> <span class="nv">pagekind</span> <span class="cp">%}</span>
  388. <a name="rest_code_611dcf30e7d4429a95ab119bf2981fdd-3"></a> <span class="cp">{%</span> <span class="k">if</span> <span class="s1">&#39;author_page&#39;</span> <span class="k">in</span> <span class="nv">pagekind</span> <span class="cp">%}</span>
  389. <a name="rest_code_611dcf30e7d4429a95ab119bf2981fdd-4"></a> <span class="p">&lt;</span><span class="nt">p</span><span class="p">&gt;</span>These posts were written by <span class="cp">{{</span> <span class="nv">author</span> <span class="cp">}}</span>. See posts by all
  390. <a name="rest_code_611dcf30e7d4429a95ab119bf2981fdd-5"></a> authors on the <span class="p">&lt;</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">&quot;/&quot;</span><span class="p">&gt;</span>front page<span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;</span>.<span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
  391. <a name="rest_code_611dcf30e7d4429a95ab119bf2981fdd-6"></a> <span class="cp">{%</span> <span class="k">elif</span> <span class="s1">&#39;tag_page&#39;</span> <span class="k">in</span> <span class="nv">pagekind</span> <span class="cp">%}</span>
  392. <a name="rest_code_611dcf30e7d4429a95ab119bf2981fdd-7"></a> <span class="p">&lt;</span><span class="nt">p</span><span class="p">&gt;</span>This is a filtered selection of posts tagged “<span class="cp">{{</span> <span class="nv">tag</span> <span class="cp">}}</span>”, visit
  393. <a name="rest_code_611dcf30e7d4429a95ab119bf2981fdd-8"></a> the <span class="p">&lt;</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">&quot;/&quot;</span><span class="p">&gt;</span>front page<span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;</span> to see all posts.<span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
  394. <a name="rest_code_611dcf30e7d4429a95ab119bf2981fdd-9"></a> <span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
  395. <a name="rest_code_611dcf30e7d4429a95ab119bf2981fdd-10"></a><span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
  396. </pre><p>List of page kinds provided by default plugins:</p>
  397. <ul class="simple">
  398. <li><p>front_page</p></li>
  399. <li><p>index</p></li>
  400. <li><p>index, archive_page</p></li>
  401. <li><p>index, author_page</p></li>
  402. <li><p>index, main_index</p></li>
  403. <li><p>index, tag_page</p></li>
  404. <li><p>list</p></li>
  405. <li><p>list, archive_page</p></li>
  406. <li><p>list, author_page</p></li>
  407. <li><p>list, tag_page</p></li>
  408. <li><p>list, tags_page</p></li>
  409. <li><p>post_page</p></li>
  410. <li><p>page_page</p></li>
  411. <li><p>story_page</p></li>
  412. <li><p>listing</p></li>
  413. <li><p>generic_page</p></li>
  414. <li><p>gallery_front</p></li>
  415. <li><p>gallery_page</p></li>
  416. </ul>
  417. </section>
  418. <section id="messages-and-translations">
  419. <h1><a class="toc-backref" href="#toc-entry-8" role="doc-backlink">Messages and Translations</a></h1>
  420. <p>The included themes are translated into a variety of languages. You can add your own translation
  421. at <a class="reference external" href="https://www.transifex.com/projects/p/nikola/">https://www.transifex.com/projects/p/nikola/</a></p>
  422. <p>If you want to create a theme that has new strings, and you want those strings to be translatable,
  423. then your theme will need a custom <code class="docutils literal">messages</code> folder.</p>
  424. </section>
  425. <section id="less-and-sass">
  426. <h1><a class="reference external" href="http://lesscss.org/">LESS</a> and <a class="reference external" href="https://sass-lang.com/">Sass</a></h1>
  427. <aside class="admonition note">
  428. <p class="admonition-title">Note</p>
  429. <p>The LESS and Sass compilers were moved to the Plugins Index in
  430. Nikola v7.0.0.</p>
  431. </aside>
  432. <p>If you want to use those CSS extensions, you can — just store your files
  433. in the <code class="docutils literal">less</code> or <code class="docutils literal">sass</code> directory of your theme.</p>
  434. <p>In order to have them work, you need to create a list of <code class="docutils literal">.less</code> or
  435. <code class="docutils literal"><span class="pre">.scss/.sass</span></code> files to compile — the list should be in a file named
  436. <code class="docutils literal">targets</code> in the respective directory (<code class="docutils literal">less</code>/<code class="docutils literal">sass</code>).</p>
  437. <p>The files listed in the <code class="docutils literal">targets</code> file will be passed to the respective
  438. compiler, which you have to install manually (<code class="docutils literal">lessc</code> which comes from
  439. the Node.js package named <code class="docutils literal">less</code> or <code class="docutils literal">sass</code> from a Ruby package aptly
  440. named <code class="docutils literal">sass</code>). Whatever the compiler outputs will be saved as a CSS
  441. file in your rendered site, with the <code class="docutils literal">.css</code> extension.</p>
  442. <aside class="admonition note">
  443. <p class="admonition-title">Note</p>
  444. <p>Conflicts may occur if you have two files with the same base name
  445. but a different extension. Pay attention to how you name your files
  446. or your site won’t build! (Nikola will tell you what’s wrong when
  447. this happens)</p>
  448. </aside>
  449. </section>