Procházet zdrojové kódy

Added first draft of theme documentation

Willem Rein Oudshoorn před 12 roky
rodič
revize
77d1881927
1 změnil soubory, kde provedl 377 přidání a 0 odebrání
  1. 377 0
      docs/themes.md

+ 377 - 0
docs/themes.md

@@ -0,0 +1,377 @@
1
+# Themes
2
+
3
+The theming support in coleslaw is very flexible, and relatively easy
4
+to use.  However it does require some knowledge of HTML, CSS and
5
+[Closure Templates][clt], and of course how coleslaw converts
6
+posts into a collection of HTML files.
7
+
8
+This document will focus mainly on the part of how coleslaw converts
9
+posts into a blog, and how you can influence the resulting HTML.
10
+
11
+## Overall Structure
12
+
13
+Conceptually the process of creating a blog with coleslaw is the following:
14
+
15
+1.  Coleslaw read a directory containt `.post` files and processes them
16
+	into HTML fragments.
17
+
18
+2.  The HTML fragments of the posts are processed with the templating engine,
19
+	to produce different HTML files.
20
+
21
+And for theming purposes, an important final step
22
+
23
+3.  A browser renders the generated HTML and styles it with CSS.
24
+
25
+The first step, the translation from markdown to HTML fragments is fixed, you cannot
26
+really influence that in a (theming) meaningful way.
27
+
28
+The theming is done in the last two steps, the templating and the CSS.
29
+
30
+Now both steps have a different role:
31
+
32
+1.  **Templating**, this determines the file structure of the
33
+	generated HTML.   This part inserts headers, footers, include
34
+    the CSS stylesheets and other resources.
35
+
36
+	This is also the place where you can add for example a table of contents
37
+    of the posts, or where the list of tags will be inserted etc.
38
+
39
+	Also, very importantly, by generating the right HTML `<div>` elements
40
+	it will make it easy to style the resulting HTML with CSS.
41
+	
42
+
43
+2.  **CSS**, this is the part which will determine the look of all the components.
44
+	For example the font and font size of the titles and sub titles.
45
+
46
+	But *CSS* is very well covered in the literature and we will not
47
+	cover how to use *CSS* in this document.  But it is good to remember that
48
+	if you are struggling to achieve a certain effect with CSS, it might
49
+    be easy to solve by modifying the template.  Either by changing the structure
50
+	of the document or by adding the right `id` or `class` attributes.
51
+
52
+
53
+**NOTE** It is not possible to change the generated file names or the generated
54
+file structure on disk.  The templating/theming support allows changing the resulting
55
+HTML but nothing more.
56
+
57
+
58
+## What Files are generated anyway?
59
+
60
+Before we dive into the templating itself, it is important to know the
61
+directory structure of the generated content, because when writing a
62
+template you need to be aware of how coleslaw lays out the blog on disk.
63
+
64
+The toplevel looks like this:
65
+
66
+
67
+    index.html
68
+    posts/
69
+    tags/
70
+    date/
71
+    static/
72
+    css/
73
+
74
+
75
+### index.html
76
+
77
+This file is the front page of the blog.  It contains a list of the most recent posts and
78
+has links to the different archives.
79
+
80
+### posts directory
81
+
82
+This directory contains an `.html` file per post.  The name of the file
83
+is the `slug` of the post.
84
+
85
+### tags directory
86
+
87
+This directory contains an `.html` file per tag.  Such a file contains
88
+all posts which contain the tag.  The name of a tag file is the `slug` of the tag.
89
+
90
+### date directory
91
+
92
+This directory contains files of the form `yyyy-mm.html` with `yyyy`
93
+the year as a 4 digit number and `mm` as a two digit month number.
94
+These files contain all the posts of the indicated month.
95
+
96
+### static directory
97
+
98
+This directory is a copy of the `static/` directory of the source folder of coleslaw.
99
+
100
+### css directory
101
+
102
+This directory is a copy of the `css/` folder of the theme.
103
+
104
+## Two type of HTML files
105
+
106
+Coleslaw generate two types of HTML files: `index` files and `post` files.
107
+Except for the files in the `posts/` directory all files are `index` files.
108
+
109
+The HTML files, as mentioned before, are created by filling in the [Closure Templates] [clt].
110
+And to generate all the HTML files there are three templates relevant:
111
+
112
+*  `base.tmpl` This template generates the outer shell of the HTML.  This is used
113
+   to generate a consist look and feel and structure for all pages in the blog.
114
+
115
+   The actual content (besides fixed headers and footers etc.) is generated by one of the
116
+   other two templates
117
+
118
+*  `index.tmpl` This template generates the content of the `index` files, remember,
119
+   these are all files containing more than one post, so including the front page.
120
+
121
+
122
+*  `post.tmpl` This generates the HTML files for the individual posts.
123
+   Remember that Coleslaw already converts the content of the individual
124
+   post to HTML by using markdown (or ReST).  So this template is NOT
125
+   used to format or convert an individual post.  This template is used
126
+   to create the HTML containing that post
127
+
128
+Visual it might be clearer this way:
129
+
130
+
131
+     INDEX HTML FILES                    INDIVIDUAL POST HTML FILES
132
+	 
133
+     |-------------------------|         |-------------------------|
134
+	 | base.tmpl               |         | base.tmpl               |
135
+	 |                         |         |                         |
136
+	 |  |-------------------|  |         | |------------------|    |
137
+     |  |  index.tmpl       |  |         | | post.tmpl        |    |
138
+	 |  |                   |  |         | |                  |    |
139
+	 |  |-------------------|  |         | |------------------|    |
140
+	 |                         |         |                         |
141
+	 |-------------------------|         |-------------------------|
142
+
143
+
144
+## Creating a Theme from Scratch (with code)
145
+
146
+### Step 1. Pick a name
147
+
148
+A theme name should satisfy two conditions:
149
+
150
+1.  It is a valid lisp symbol name (not containing `:`)
151
+2.  It is a valid directory name.
152
+
153
+So for this example lets pick `trivial`.
154
+
155
+### Step 2. Create the right directory
156
+
157
+Now we need to create a directory containing the theme files.
158
+In the coleslaw system directory should be a directory called `themes/`.
159
+The directory for the theme files is a sub directory of the `themes/` directory,
160
+named after the theme name we picked in step 1.
161
+
162
+So in our case, we have to create the directory `themes/trivial/`.
163
+
164
+### Step 3. Create the 3 template files
165
+
166
+As described above, we need the 3 template files `base.tmpl`, `post.tmpl` and `index.tmpl`.
167
+Before we customize them, create the with the following content:
168
+
169
+base.tmpl:
170
+	 
171
+       {namespace coleslaw.theme.trivial}
172
+       {template base}
173
+       {/template}
174
+	
175
+
176
+post.tmpl
177
+
178
+       {namespace coleslaw.theme.trivial}
179
+       {template post}
180
+       {/template}
181
+
182
+index.tmpl
183
+
184
+       {namespace coleslaw.theme.trivial}
185
+       {template index}
186
+       {/template}
187
+
188
+
189
+The first line in these files, declares the namespace of the template.  The namespace must be
190
+`coleslaw.theme.` followed by the theme name.
191
+
192
+The remaining two lines `{template ...}{/template ...}` create the empty templates.
193
+
194
+### Step 4. Test
195
+
196
+This is enough to make coleslaw happy.  You can now use the new `trivial` theme in coleslaw
197
+by changing the `:theme` in `.coleslawrc` file to `trivial`.
198
+
199
+If you do this and generate the blog with `(coleslaw:main "")`, coleslaw will not complain
200
+and generate all the post files, tag files and front page files as normal.  Except that all
201
+these files are empty.
202
+
203
+The HTML files are empty because the templates are empty.
204
+
205
+### Step 5. Generating valid HTML
206
+
207
+The `base.tmpl` generates all the HTML pages, so if we change the base template to:
208
+
209
+       {namespace coleslaw.theme.trivial}
210
+       {template base}
211
+          <html>
212
+     	   <head><title>Trivial Theme For Coleslaw</title></head>
213
+     	   <body>
214
+     	   <h1>All my pages have this title</h1>
215
+     	   </body>
216
+    	   </html>
217
+       {/template}
218
+
219
+We will generate valid HTML.  Of course every page is still the same, but at least
220
+it shows that the templating engine works.
221
+
222
+
223
+### Intermezzo I, The Templating Language
224
+
225
+The templating language is documented at [Google closure templates][clt].
226
+However as a short primer:
227
+
228
+*   Everyting is outputed literally, except template commands
229
+*   Template commands are enclosed in `{` and `}`
230
+*   Variables, which are provided by coleslaw are referenced
231
+    as `$variable.key` inside a template command.
232
+	So to insert a variable you have to use `{$variable.key}`.
233
+
234
+	Variables are either simple variables, which are referenced as `{$var}`
235
+	Or are indexed by a key and written as `{$var.key}`.
236
+	
237
+*   If statements are written as
238
+
239
+           {if ...} ... {else} ... {/if}
240
+
241
+	Typical examples are:
242
+
243
+           {if $injections.body} ... {/fi}
244
+
245
+    Or
246
+
247
+           {if not isLast($link)} ... {/fi}
248
+
249
+*   Loops are typically written as
250
+
251
+           {foreach $var in $index.posts} ... {/foreach}
252
+
253
+
254
+
255
+
256
+### Intermezzo II, Variables provided by coleslaw
257
+
258
+The variable that is available in all templates is:
259
+
260
+- **config** This contains the .coleslawrc content
261
+
262
+#### Base Template Variables
263
+
264
+- **raw**          the HTML generated by the sub templates, `index.tmpl` or `post.tmpl`
265
+- **content**      the data which was used to generate **raw** HTML.
266
+- **pubdate**      A string containing the publication date
267
+- **injections**   A list containg all injections.  Injections are for the plugins to
268
+                   communicate additional content to be included.
269
+
270
+
271
+#### Index Template Variables
272
+
273
+- **tags**         A list containing all the tags, A tag has values `.name` and `.slug`.
274
+- **months**       A list of all months for which there are posts.  This is a list
275
+                   of strings.  The strings are formatted as `yyyy-mm`.
276
+- **index**        This is the meat of the content.  This variable has as keys
277
+ - `id`, the name of the page that will be rendered
278
+ - `posts` a list of posts (see below)
279
+ - `title` The title under which this index is know.  This should be used
280
+	       to display a title for the user.
281
+- **prev**         If this index file is part of a chain, the `id`
282
+                   of the previous index html in the chain.
283
+				   If this is the first file, the value will be empty.
284
+- **next**         If this index file is part of a chain, the `id`
285
+                   of the next index html in the chain.
286
+				   If this is the last file, the value will be empty.
287
+
288
+
289
+#### Post Template Variable
290
+
291
+- **prev**          
292
+- **next**          
293
+- **post**         All these variables are of the same type, they represent a post.
294
+                   The **prev** and **next** are the post before this one, or the one
295
+				   after this one when put in chronological order.
296
+                   These variables have the following keys
297
+   - `tags` a list of tags (a tag has keys `name` and `slug`)
298
+   - `slug` the slug of the post
299
+   - `date` the date of posting
300
+   - `text` the HTML version of the posts body.
301
+   - `title` The title of the post
302
+                   
303
+
304
+### Step 6.  Including the content
305
+
306
+We improve the `base.tmpl` to include the content generated by the sub templates.
307
+This is done by adding the line `{$raw |noAutoescape}` to the template in the body section.
308
+
309
+The `|noAutoescape` is added because the `$raw` variable is already html and we do
310
+not want the templating engine to escape the html.
311
+
312
+So the `base.tmpl` now looks like this:
313
+
314
+       {namespace coleslaw.theme.trivial}
315
+       {template base}
316
+
317
+	   <html>
318
+	   <head><title>Trivial Theme For Coleslaw</title></head>
319
+	   <body>
320
+	   <h1>All my pages have this title</h1>
321
+     	   {$raw |noAutoescape}
322
+	   </body>
323
+	   </html>
324
+
325
+       {/template}
326
+
327
+If we run this through coleslaw we do not see any difference and this is because
328
+we have not modified the `index.tmpl` and the `post.tmpl`.
329
+
330
+A simple `index.tmpl` looks like this
331
+
332
+     {namespace coleslaw.theme.trivial}
333
+     {template index}
334
+		 {foreach $post in $index.posts}
335
+		    <h1>{$post.title}</h1>
336
+		    {$post.text |noAutoescape}
337
+		 {/foreach}
338
+     {/template}
339
+
340
+     
341
+And a simple `post.tmpl` is similarly
342
+
343
+    {namespace coleslaw.theme.trivial}
344
+    {template post}
345
+    <h1>{$post.title}</h1>
346
+    {$post.text |noAutoescape}
347
+    {/template}
348
+
349
+### Wrapup of Example
350
+
351
+Basically all the files are now populated with content.  There are a few huge gaps still, for example there is no
352
+linking between the pages.  So although the archives are fully populated, you cannot get there from the
353
+front page, but if you know the URL it is there.
354
+
355
+However linking is not very difficult see next section.
356
+
357
+
358
+## Note on adding links
359
+
360
+As mentioned in the beginning, most files have a file name which is a slug of some sort. So if you want to create a link
361
+to a tag file you should do something like this:
362
+
363
+      <a href="${config.domain}/tags/$tag.slug">$tag.name</a>
364
+
365
+
366
+## Note on adding style sheets
367
+
368
+Style sheets are nothing special.  In order to get them to work, you
369
+have to add a `css` folder in your theme folder.  The content of this folder
370
+will be copied to the `css` folder at the place where the `html` is generated.
371
+
372
+Now to actual use them, you have to include in the head section of the html
373
+(which is in the `base.tmpl`) a link to include the CSS.
374
+
375
+
376
+
377
+[clt]: https://developers.google.com/closure/templates/