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

internals.html 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <p class="lead">When trying to guide someone into adding a feature in Nikola, it hit me that
  2. while the way it's structured makes sense <strong>to me</strong> it is far from obvious.</p>
  3. <p>So, this is a short document explaining what each piece of Nikola does and
  4. how it all fits together.</p>
  5. <dl>
  6. <dt>Nikola is a Pile of Plugins</dt>
  7. <dd><p>Most of Nikola is implemented as plugins using <a class="reference external" href="http://yapsy.sourceforge.net/">Yapsy</a>.
  8. You can ignore that they are plugins and just think of them as regular python
  9. modules and packages with a funny little <code class="docutils literal">.plugin</code> file next to them.</p>
  10. <p>So, 90% of the time, what you want to do is either write a new plugin or extend
  11. an existing one.</p>
  12. <p>There are several kinds of plugins, all implementing interfaces defined in
  13. <code class="docutils literal">nikola/plugin_categories.py</code> and documented in
  14. <a class="reference external" href="https://getnikola.com/extending.html">Extending Nikola</a></p>
  15. <p>If your plugin has a dependency, please make sure it doesn't make Nikola
  16. throw an exception when the dependency is missing. Try to fail gracefully
  17. with an informative message.</p>
  18. </dd>
  19. <dt>Commands are plugins</dt>
  20. <dd><p>When you use <code class="docutils literal">nikola foo</code> you are using the plugin <code class="docutils literal">command/foo</code>. Those are
  21. used to extend Nikola's command line. Their interface is defined in the <code class="docutils literal">Command</code>
  22. class. They take options and arguments and do whatever you want, so go wild.</p>
  23. </dd>
  24. <dt>The <code class="docutils literal">build</code> command is special</dt>
  25. <dd><p>The <code class="docutils literal">build</code> command triggers a whole lot of things, and is the core of Nikola
  26. because it's the one that you use to build sites. So it deserves its own section.</p>
  27. </dd>
  28. </dl>
  29. <section id="the-build-command">
  30. <h1>The Build Command</h1>
  31. <p>Nikola's goal is similar, deep at heart, to a Makefile. Take sources, compile them
  32. into something, in this case a website. Instead of a Makefile, Nikola uses
  33. <a class="reference external" href="https://pydoit.org">doit</a>.</p>
  34. <p>Doit has the concept of &quot;tasks&quot;. The 1 minute summary of tasks is that they have:</p>
  35. <dl class="simple">
  36. <dt>actions</dt>
  37. <dd><p>What the task <strong>does</strong>. For example, convert a markdown document into HTML.</p>
  38. </dd>
  39. <dt>dependencies</dt>
  40. <dd><p>If this file changes, then we need to redo the actions. If this configuration
  41. option changes, redo it, etc.</p>
  42. </dd>
  43. <dt>targets</dt>
  44. <dd><p>Files that the action generates. No two actions can have the same targets.</p>
  45. </dd>
  46. <dt>basename:name</dt>
  47. <dd><p>Each task is identified by either a name or a basename:name pair.</p>
  48. </dd>
  49. </dl>
  50. <aside class="sidebar">
  51. <p class="sidebar-title">More about tasks</p>
  52. <p>If you ever want to do your own tasks, you really should read the doit
  53. <a class="reference external" href="https://pydoit.org/tasks.html">documentation on tasks</a>.</p>
  54. <p>Notably, by default doit redirects <code class="docutils literal">stdout</code> and <code class="docutils literal">stderr</code>. To get a
  55. proper PDB debugging shell, you need to use doit's own
  56. <a class="reference external" href="https://pydoit.org/tools.html#set-trace">set_trace</a> function.</p>
  57. </aside>
  58. <p>So, what Nikola does, when you use the build command, is to read the
  59. configuration <code class="docutils literal">conf.py</code> from the current folder, instantiate
  60. the <code class="docutils literal">Nikola</code> class, and have it generate a whole list of tasks for doit
  61. to process. Then doit will decide which tasks need doing, and do them, in
  62. the right order.</p>
  63. <p>The place where the tasks are generated is in <code class="docutils literal">Nikola.gen_tasks</code>, which collects tasks
  64. from all the plugins inheriting <code class="docutils literal">BaseTask</code>, massages them a bit, then passes them
  65. to doit.</p>
  66. <p>So, if you want things to happen on <code class="docutils literal">build</code> you want to create a Task plugin, or extend
  67. one of the existing ones.</p>
  68. <aside class="sidebar">
  69. <p class="sidebar-title">Tests</p>
  70. <p>While Nikola is not a hardcore TDD project, we like tests. So, please add them if you can.
  71. You can write unit tests or integration tests. (Doctests are not supported
  72. anymore due to fragility.)</p>
  73. </aside>
  74. </section>
  75. <section id="posts-and-pages">
  76. <h1>Posts and Pages</h1>
  77. <p>Nikola has a concept of posts and pages. Both are more or less the same thing, except
  78. posts are added into RSS feeds and pages are not. All of them are in a list called
  79. &quot;the timeline&quot; formed by objects of class <code class="docutils literal">Post</code>.</p>
  80. <p>When you are creating a task that needs the list of posts and/or pages (for example,
  81. the RSS creation plugin) on task execution time, your plugin should call <code class="docutils literal">self.site.scan_posts()</code>
  82. in <code class="docutils literal">gen_tasks</code> to ensure the timeline is created and available in
  83. <code class="docutils literal">self.site.timeline</code>. You should not modify the timeline, because it will cause consistency issues.</p>
  84. <aside class="sidebar">
  85. <p class="sidebar-title">scan_posts</p>
  86. <p>The <code class="docutils literal">Nikola.scan_posts</code> function can be used in plugins to force the
  87. timeline creation, for example, while creating the tasks.</p>
  88. </aside>
  89. <p>Your plugin can use the timeline to generate &quot;stuff&quot; (technical term). For example,
  90. Nikola comes with plugins that use the timeline to create a website (surprised?).</p>
  91. <p>The workflow included with nikola is as follows (incomplete!):</p>
  92. <ol class="arabic simple">
  93. <li><p>The post is assigned a compiler based on its extension and the <code class="docutils literal">COMPILERS</code> option.</p></li>
  94. <li><p>The compiler is applied to the post data and a &quot;HTML fragment&quot; is produced. That
  95. fragment is stored in a cache (the <code class="docutils literal">posts</code> plugin).</p></li>
  96. <li><p>The configured theme has templates (and a template engine), which are applied to the post's
  97. HTML fragment and metadata (the <code class="docutils literal">pages</code> plugin).</p></li>
  98. <li><p>The original sources for the post are copied to some accessible place (the <code class="docutils literal">sources</code> plugin).</p></li>
  99. <li><p>If the post is tagged, some pages and RSS feeds for each tag are updated (the <code class="docutils literal">tags</code> plugin).</p></li>
  100. <li><p>If the post is new, it's included in the blog's RSS feed (the <code class="docutils literal">rss</code> plugin).</p></li>
  101. <li><p>The post is added in the right place in the index pages for the blog (the <code class="docutils literal">indexes</code> plugin).</p></li>
  102. <li><p>CSS/JS/Images for the theme are put in the right places (the <code class="docutils literal">copy_assets</code> and <code class="docutils literal">bundles</code> plugins).</p></li>
  103. <li><p>A File describing the whole site is created (the <code class="docutils literal">sitemap</code> plugin).</p></li>
  104. </ol>
  105. <p>You can add whatever you want to that list: just create a plugin for it.</p>
  106. <p>You can also expand Nikola's capabilities at several points:</p>
  107. <dl class="simple">
  108. <dt>compilers</dt>
  109. <dd><p>Nikola supports a variety of markups. If you want to add another one, you need to create
  110. a <code class="docutils literal">Compiler</code> plugin.</p>
  111. </dd>
  112. <dt>templates</dt>
  113. <dd><p>Nikola's themes can use Jinja2 or Mako templates. If you prefer another template system,
  114. you have to create a <code class="docutils literal">TemplateSystem</code> plugin.</p>
  115. </dd>
  116. <dt>themes</dt>
  117. <dd><p>To change how the generated site looks, you can create custom themes.</p>
  118. </dd>
  119. </dl>
  120. <p>And of course, you can also replace or extend each of the existing plugins.</p>
  121. <section id="nikola-architecture">
  122. <h2>Nikola Architecture</h2>
  123. <a class="reference external image-reference" href="https://getnikola.com/images/architecture.png"><img alt="https://getnikola.com/images/architecture.thumbnail.png" src="https://getnikola.com/images/architecture.thumbnail.png" /></a>
  124. </section>
  125. </section>