Almost-minimal filesystem based blog.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

472 lines
14 KiB

7 years ago
  1. =pod
  2. =head1 NAME
  3. App::WRT - WRiting Tool, a static site generator and related utilities
  4. =for HTML <a href="https://travis-ci.org/brennen/wrt"><img src="https://travis-ci.org/brennen/wrt.svg?branch=master"></a>
  5. =head1 SYNOPSIS
  6. Using the commandline tools:
  7. $ mkdir project
  8. $ cd project
  9. $ wrt init # set up some defaults
  10. $ wrt display new # print html for new posts to stdout
  11. $ wrt render-all # publish html to project/public/
  12. Using App::WRT in library form:
  13. #!/usr/bin/env perl
  14. use App::WRT;
  15. my $w = App::WRT->new(
  16. entry_dir => 'archives',
  17. url_root => '/',
  18. # etc.
  19. );
  20. print $w->display(@ARGV);
  21. =head1 INSTALLING
  22. It's possible but not likely this would run on a Perl as old as 5.10.0. In
  23. practice, I know that it works under 5.20.2. It should be fine on any
  24. reasonably modern Linux distribution, and may also be fine on MacOS or a BSD of
  25. your choosing.
  26. To install the latest development version from the main repo:
  27. $ git clone https://github.com/brennen/wrt.git
  28. $ cd wrt
  29. $ perl Build.PL
  30. $ ./Build installdeps
  31. $ ./Build test
  32. $ ./Build install
  33. To install the latest version released on CPAN:
  34. $ cpanm App::WRT
  35. Or:
  36. $ cpan -i App::WRT
  37. You will likely need to use C<sudo> or C<su> to get a systemwide install.
  38. =head1 DESCRIPTION
  39. This started life as C<display.pl>, a simple script to concatenate fragments of
  40. handwritten HTML by date. It has since accumulated several of the usual weblog
  41. features (lightweight markup, feed generation, embedded Perl, poetry tools,
  42. image galleries, and ill-advised dependencies), but the basic idea hasn't
  43. changed that much.
  44. The C<wrt> utility now generates static HTML files, instead of expecting to
  45. run as a CGI script. This is a better idea, for the most part.
  46. The C<App::WRT> module will work with FastCGI, if called from the appropriate
  47. wrapper script, such as C<wrt-fcgi>.
  48. By default, entries are stored in a simple directory tree under C<entry_dir>.
  49. Like:
  50. archives/2001/1/1
  51. archives/2001/1/2/index
  52. archives/2001/1/2/sub_entry
  53. Which will publish files like so:
  54. public/index.html
  55. public/all/index.html
  56. public/2001/index.html
  57. public/2001/1/index.html
  58. public/2001/1/1/index.html
  59. public/2001/1/2/index.html
  60. public/2001/1/2/sub_entry/index.html
  61. Contents will be generated for each year and for the entire collection of dated
  62. entries. Month indices will consist of all entries for that month. A
  63. top-level index file will consist of the most recent month's entries.
  64. It's possible (although not as flexible as it ought to be) to redefine the
  65. directory layout. (See C<%default{entry_map}> below.)
  66. An entry may be either a plain text file, or a directory containing several
  67. files. If it's a directory, a file named "index" will be treated as the text
  68. of the entry, and all other lower-case filenames without extensions will be
  69. treated as sub-entries or documents within that entry, and displayed
  70. accordingly. Links to certain other filetypes will be displayed as well.
  71. Directories may be nested to an arbitrary depth, although it's probably not a
  72. good idea to go very deep with the current display logic.
  73. A PNG or JPEG file with a name like
  74. 2001/1/1.icon.png
  75. 2001/1/1/index.icon.png
  76. 2001/1/1/whatever.icon.png
  77. 2001/1/1/whatever/index.icon.png
  78. will be treated as an icon for the corresponding entry file.
  79. =head2 MARKUP
  80. Entries may consist of hand-written HTML (to be passed along without further
  81. interpretation), a supported form of lightweight markup, or some combination
  82. thereof. Actually, an entry may consist of any darn thing you please, as long
  83. as Perl will agree that it is text, but presumably you're going to be feeding
  84. this to a browser.
  85. Header tags (<h1>, <h2>, etc.) will be used to display titles in feeds and
  86. other places.
  87. Other special markup is indicated by a variety of HTML-like container tags.
  88. B<Embedded Perl> - evaluated and replaced by whatever value you return
  89. (evaluated in a scalar context):
  90. <perl>my $dog = "Ralph."; return $dog;</perl>
  91. This code is evaluated before any other processing is done, so you can return
  92. any other markup understood by the script and have it handled appropriately.
  93. B<Interpolated variables> - actually keys to the hash underlying the App::WRT
  94. object, for the moment:
  95. <perl>$self->title("About Ralph, My Dog"); return '';</perl>
  96. <p>The title is <em>${title}</em>.</p>
  97. This is likely to change at some point, so don't build anything too elaborate
  98. on it.
  99. Embedded code and variables are intended only for use in the F<template> file,
  100. where it's handy to drop in titles or conditionalize aspects of a layout. You
  101. want to be careful with this sort of thing - it's useful in small doses, but
  102. it's also a maintainability nightmare waiting to happen. (WordPress, I am
  103. looking at you.)
  104. B<Several forms of lightweight markup>:
  105. <markdown>John Gruber's Markdown, by way of
  106. Text::Markdown</markdown>
  107. <textile>Dean Allen's Textile, via Brad Choate's
  108. Text::Textile.</textile>
  109. <freeverse>An easy way to
  110. get properly broken lines
  111. plus -- en and em dashes ---
  112. for poetry and such.</freeverse>
  113. B<And a couple of shortcuts>:
  114. <image>filename.ext
  115. alt text, if any</image>
  116. <list>
  117. one list item
  118. another list item
  119. </list>
  120. As it stands, freeverse, image, and list are not particularly robust.
  121. =head2 TEMPLATES
  122. A single template, specified by the C<template_dir> and C<template> config
  123. values, is used to render all pages. See F<example/templates/basic> for an
  124. example, or run C<wrt init> in an empty directory and look at
  125. F<templates/default>.
  126. Here's a short example:
  127. <!DOCTYPE html>
  128. <html>
  129. <head>
  130. <meta charset="UTF-8">
  131. <title>${title_prefix} - ${title}</title>
  132. </head>
  133. <body>
  134. ${content}
  135. </body>
  136. </html>
  137. Within templates, C<${foo}> will be replaced with the corresponding
  138. configuration value. C<${content}> will always be set to the content of the
  139. current entry.
  140. =head2 CONFIGURATION
  141. Configuration is read from a F<wrt.json> in the directory where the C<wrt>
  142. utility is invoked, or can (usually) be specified with the C<--config> option.
  143. See F<example/wrt.json> for a sample configuration.
  144. Under the hood, configuration is done by combining a hash called C<%default>
  145. with values pulled out of the JSON file. Most defaults can be overwritten
  146. from the config file, but changing some would require writing Perl, since
  147. they contain things like subroutine references.
  148. =over
  149. =item %default
  150. Here's a verbatim copy of C<%default>, with some commentary about values.
  151. my %default = (
  152. root_dir => '.', # dir for wrt repository
  153. entry_dir => 'archives', # dir for entry files
  154. publish_dir => 'public', # dir to publish site to
  155. url_root => "$0?", # root URL for building links
  156. image_url_root => '', # same for images
  157. template_dir => 'templates', # dir for template files
  158. template => 'default', # template to use
  159. title => '', # current title (used in template)
  160. title_prefix => '', # a string to slap in front of titles
  161. stylesheet_url => undef, # path to a CSS file (used in template)
  162. favicon_url => undef, # path to a favicon (used in template)
  163. feed_alias => 'feed', # what entry path should correspond to feed?
  164. author => undef, # author name (used in template, feed)
  165. description => undef, # site description (used in template)
  166. content => undef, # place to stash content for templates
  167. embedded_perl => 1, # evaluate embedded <perl> tags?
  168. default_entry => 'new', # what to display if no entry specified
  169. # A license string for site content:
  170. license => 'public domain',
  171. # List of years for the menu:
  172. year_list => [ reverse(1997..(get_date('year') + 1900)) ],
  173. # What gets considered an entry _path_:
  174. entrypath_expr => qr/^ ([a-z0-9_\/-]+) $/x,
  175. # What gets considered a subentry file (slightly misleading
  176. # terminology here):
  177. subentry_expr => qr/^[0-9a-z_-]+(\.(tgz|zip|tar[.]gz|gz|txt))?$/,
  178. # We'll show links for these, but not display them inline:
  179. binfile_expr => qr/[.](tgz|zip|tar[.]gz|gz|txt|pdf)$/,
  180. );
  181. =item $default{entry_map}
  182. A hash which will dispatch entries matching various regexen to the appropriate
  183. output methods. The default looks something like this:
  184. nnnn/[nn/nn/]doc_name - a document within a day.
  185. nnnn/nn/nn - a specific day.
  186. nnnn/nn - a month.
  187. nnnn - a year.
  188. doc_name - a document in the root directory.
  189. You can re-map things to an arbitrary archive layout.
  190. Since the entry map is a hash, and handle() simply loops over its keys, there
  191. is no guaranteed precedence of patterns. Be extremely careful that no entry
  192. will match more than one pattern, or you will wind up with unexpected behavior.
  193. A good way to ensure that this does not happen is to use patterns like:
  194. qr(
  195. ^ # start of string
  196. [0-9/]{4}/ # year
  197. [0-9]{1,2}/ # month
  198. [0-9]{1,2] # day
  199. $ # end of string
  200. )x
  201. ...always marking the start and end of the string explicitly.
  202. This may eventually be rewritten to use an array so that the order can be
  203. explicitly specified.
  204. =item $default{entry_descriptions}
  205. A hashref which contains a map of entry titles to entry descriptions.
  206. =back
  207. =head2 METHODS AND INTERNALS
  208. For no bigger than this thing is, the internals are convoluted. Because it's
  209. spaghetti code originally written in a now-archaic language by a teenager who
  210. didn't know how to program.
  211. =over
  212. =item new_from_file($config_file)
  213. Takes a filename to pull JSON config data out of, and returns a new App::WRT
  214. instance with the parameters set in that file.
  215. =item new(%params)
  216. Get a new WRT object with the specified parameters set.
  217. =item display($entry1, $entry2, ...)
  218. Return a string containing the given entries, which are in the form of
  219. date/entry strings. If no parameters are given, default to default_entry().
  220. display() expands aliases ("new" and "all", for example) as necessary, collects
  221. output from handle($entry), and wraps the whole thing in a template file.
  222. =item handle($entry)
  223. Return the text of an individual entry.
  224. =begin digression
  225. =item A digression about each()
  226. I once spent a lot of time chasing down a bug caused by a while loop in this
  227. method. Specifically, I was using while to iterate over the entry_map hash.
  228. Since C<$self->entry_map> returns a reference to the same hash each time, every
  229. other request was finding C<each()> mid-way through iterating over this hash.
  230. I initially solved this by copying the hash into a local one called C<%map>
  231. every time C<handle()> was called. I could also have called C<keys> or
  232. C<values> on the anonymous hash, as these reset C<each()>.
  233. Presently I'm not using each() or an explicit loop, so this probably doesn't
  234. make a whole lot of sense in the context of the existing code.
  235. =end digression
  236. =item expand_option($option)
  237. Expands/converts 'all' and 'new' to appropriate values.
  238. =item recent_month()
  239. Tries to find the most recent month in the archive.
  240. If a year file is text, returns that instead.
  241. =item fulltext()
  242. Returns the full text of all entries, in order.
  243. =item link_bar(@extra_links)
  244. Returns a little context-sensitive navigation bar.
  245. =item month_before($this_month)
  246. Return the month before the given month in the archive.
  247. Very naive; there has got to be a smarter way.
  248. =item year($year)
  249. List out the updates for a year.
  250. =item month($month)
  251. Prints the entries in a given month (nnnn/nn).
  252. =item entry_wrapped($entry, $level)
  253. Wraps entry() in entry_markup.
  254. =item entry_stamped($entry, $level)
  255. Wraps entry() + a datestamp in entry_markup()
  256. =item entry_topic_list($entry)
  257. Get a list of topics (by tag-* files) for the entry. This hardcodes a
  258. p1k3-specific thing, and is dumb.
  259. =item entry($entry)
  260. Returns the contents of a given entry. Calls dir_list
  261. and icon_markup. Recursively calls itself.
  262. =item get_sub_entries($entry_loc)
  263. Returns "sub entries" based on the C<subentry_expr> regexp.
  264. =item list_contents($entry, @entries)
  265. Returns links (potentially with icons) for a set of sub-entries within an
  266. entry.
  267. =item icon_markup($entry, $alt)
  268. Check if an icon exists for a given entry if so, return markup to include it.
  269. Icons are PNG or JPEG image files following a specific naming convention:
  270. index.icon.[png|jp(e)g] for directories
  271. [filename].icon.[png|jp(e)g] for flat text files
  272. Calls image_size, uses filename to determine type.
  273. =item datestamp($entry)
  274. Returns a nice html datestamp / breadcrumbs for a given entry.
  275. =item fragment_slurp($file)
  276. Read a text fragment, call line_parse() and eval_perl() to take care of funky
  277. markup and interpreting embedded code, and then return it as a string. Takes
  278. one parameter, the name of the file, and returns '' if it's not an extant text
  279. file.
  280. This might be the place to implement an in-memory cache for FastCGI or mod_perl
  281. environments. The trick is that the results for certain files shouldn't be
  282. cached because they contain embedded code.
  283. =item month_name($number)
  284. Turn numeric dates into English.
  285. =item root_locations($file)
  286. Given a file/entry, return the appropriate concatenations with
  287. entry_dir and url_root.
  288. =item local_path($file)
  289. Return an absolute path for a given file. Called by root_locations.
  290. Arguably this is stupid and inefficient.
  291. =item feed_print($month)
  292. Return an Atom feed of entries for a month. Defaults to the most
  293. recent month in the archive.
  294. Called from handle(), requires XML::Atom::SimpleFeed.
  295. =back
  296. =head1 SEE ALSO
  297. walawiki.org, Blosxom, rassmalog, Text::Textile, XML::Atom::SimpleFeed,
  298. Image::Size, CGI::Fast, and about a gazillion static site generators.
  299. =head1 AUTHOR
  300. Copyright 2001-2017 Brennen Bearnes
  301. =head1 LICENSE
  302. wrt is free software; you can redistribute it and/or modify
  303. it under the terms of the GNU General Public License as published by
  304. the Free Software Foundation; either version 2 of the License, or
  305. (at your option) any later version.
  306. This program is distributed in the hope that it will be useful,
  307. but WITHOUT ANY WARRANTY; without even the implied warranty of
  308. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  309. GNU General Public License for more details.
  310. You should have received a copy of the GNU General Public License
  311. along with this program. If not, see <http://www.gnu.org/licenses/>.