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.

394 lines
9.5 KiB

cache rendered html; extract titles; all as utf-8 This stashes the HTML version of every entry in memory and uses Mojo::DOM to extract headers from the markup for use as titles. Titles are displayed in $self->{page_navigation}, now available inside templates as ${page_navigation}. In order to keep Mojo::DOM from choking on other input, it uses the open pragma to open everything as UTF-8 by default, which also eliminates a whole class of character encoding bugs and removes some fiddling with Encode::decode() from feed_print(). This is all obviously a more memory-intensive, but caching the markup turns out to have the side effect of making it much faster to render even a large site, probably as much as anything because the HTML in question is only getting generated once per entry instead of (potentially) 2-3 times. This commit isn't very atomic. In the process of roughing it out and testing it, I made a small pile of minor but potentially breaking changes: - Removed entry_map from settings and hardcoded handling of various types of entry as some if-statements instead. - Removed embedded_perl flag in settings - was always turned on in practice, and wasn't very coherent since templating would have broken without it. - bin/wrt-display - now handles the "feed" alias correctly - EntryStore: now supports retrieving values for properties with prop_value() - this isn't currently used, but it seems like a reasonable extension of the property idea. - Added `wrt ls --with-titles`. - Added dependency versions to Build.PL. - Refactored Markup's line_parse() a little. - Refactored some tests to give cleaner / more useful output. - Renamed default template file to "default".
4 years ago
cache rendered html; extract titles; all as utf-8 This stashes the HTML version of every entry in memory and uses Mojo::DOM to extract headers from the markup for use as titles. Titles are displayed in $self->{page_navigation}, now available inside templates as ${page_navigation}. In order to keep Mojo::DOM from choking on other input, it uses the open pragma to open everything as UTF-8 by default, which also eliminates a whole class of character encoding bugs and removes some fiddling with Encode::decode() from feed_print(). This is all obviously a more memory-intensive, but caching the markup turns out to have the side effect of making it much faster to render even a large site, probably as much as anything because the HTML in question is only getting generated once per entry instead of (potentially) 2-3 times. This commit isn't very atomic. In the process of roughing it out and testing it, I made a small pile of minor but potentially breaking changes: - Removed entry_map from settings and hardcoded handling of various types of entry as some if-statements instead. - Removed embedded_perl flag in settings - was always turned on in practice, and wasn't very coherent since templating would have broken without it. - bin/wrt-display - now handles the "feed" alias correctly - EntryStore: now supports retrieving values for properties with prop_value() - this isn't currently used, but it seems like a reasonable extension of the property idea. - Added `wrt ls --with-titles`. - Added dependency versions to Build.PL. - Refactored Markup's line_parse() a little. - Refactored some tests to give cleaner / more useful output. - Renamed default template file to "default".
4 years ago
cache rendered html; extract titles; all as utf-8 This stashes the HTML version of every entry in memory and uses Mojo::DOM to extract headers from the markup for use as titles. Titles are displayed in $self->{page_navigation}, now available inside templates as ${page_navigation}. In order to keep Mojo::DOM from choking on other input, it uses the open pragma to open everything as UTF-8 by default, which also eliminates a whole class of character encoding bugs and removes some fiddling with Encode::decode() from feed_print(). This is all obviously a more memory-intensive, but caching the markup turns out to have the side effect of making it much faster to render even a large site, probably as much as anything because the HTML in question is only getting generated once per entry instead of (potentially) 2-3 times. This commit isn't very atomic. In the process of roughing it out and testing it, I made a small pile of minor but potentially breaking changes: - Removed entry_map from settings and hardcoded handling of various types of entry as some if-statements instead. - Removed embedded_perl flag in settings - was always turned on in practice, and wasn't very coherent since templating would have broken without it. - bin/wrt-display - now handles the "feed" alias correctly - EntryStore: now supports retrieving values for properties with prop_value() - this isn't currently used, but it seems like a reasonable extension of the property idea. - Added `wrt ls --with-titles`. - Added dependency versions to Build.PL. - Refactored Markup's line_parse() a little. - Refactored some tests to give cleaner / more useful output. - Renamed default template file to "default".
4 years ago
cache rendered html; extract titles; all as utf-8 This stashes the HTML version of every entry in memory and uses Mojo::DOM to extract headers from the markup for use as titles. Titles are displayed in $self->{page_navigation}, now available inside templates as ${page_navigation}. In order to keep Mojo::DOM from choking on other input, it uses the open pragma to open everything as UTF-8 by default, which also eliminates a whole class of character encoding bugs and removes some fiddling with Encode::decode() from feed_print(). This is all obviously a more memory-intensive, but caching the markup turns out to have the side effect of making it much faster to render even a large site, probably as much as anything because the HTML in question is only getting generated once per entry instead of (potentially) 2-3 times. This commit isn't very atomic. In the process of roughing it out and testing it, I made a small pile of minor but potentially breaking changes: - Removed entry_map from settings and hardcoded handling of various types of entry as some if-statements instead. - Removed embedded_perl flag in settings - was always turned on in practice, and wasn't very coherent since templating would have broken without it. - bin/wrt-display - now handles the "feed" alias correctly - EntryStore: now supports retrieving values for properties with prop_value() - this isn't currently used, but it seems like a reasonable extension of the property idea. - Added `wrt ls --with-titles`. - Added dependency versions to Build.PL. - Refactored Markup's line_parse() a little. - Refactored some tests to give cleaner / more useful output. - Renamed default template file to "default".
4 years ago
cache rendered html; extract titles; all as utf-8 This stashes the HTML version of every entry in memory and uses Mojo::DOM to extract headers from the markup for use as titles. Titles are displayed in $self->{page_navigation}, now available inside templates as ${page_navigation}. In order to keep Mojo::DOM from choking on other input, it uses the open pragma to open everything as UTF-8 by default, which also eliminates a whole class of character encoding bugs and removes some fiddling with Encode::decode() from feed_print(). This is all obviously a more memory-intensive, but caching the markup turns out to have the side effect of making it much faster to render even a large site, probably as much as anything because the HTML in question is only getting generated once per entry instead of (potentially) 2-3 times. This commit isn't very atomic. In the process of roughing it out and testing it, I made a small pile of minor but potentially breaking changes: - Removed entry_map from settings and hardcoded handling of various types of entry as some if-statements instead. - Removed embedded_perl flag in settings - was always turned on in practice, and wasn't very coherent since templating would have broken without it. - bin/wrt-display - now handles the "feed" alias correctly - EntryStore: now supports retrieving values for properties with prop_value() - this isn't currently used, but it seems like a reasonable extension of the property idea. - Added `wrt ls --with-titles`. - Added dependency versions to Build.PL. - Refactored Markup's line_parse() a little. - Refactored some tests to give cleaner / more useful output. - Renamed default template file to "default".
4 years ago
cache rendered html; extract titles; all as utf-8 This stashes the HTML version of every entry in memory and uses Mojo::DOM to extract headers from the markup for use as titles. Titles are displayed in $self->{page_navigation}, now available inside templates as ${page_navigation}. In order to keep Mojo::DOM from choking on other input, it uses the open pragma to open everything as UTF-8 by default, which also eliminates a whole class of character encoding bugs and removes some fiddling with Encode::decode() from feed_print(). This is all obviously a more memory-intensive, but caching the markup turns out to have the side effect of making it much faster to render even a large site, probably as much as anything because the HTML in question is only getting generated once per entry instead of (potentially) 2-3 times. This commit isn't very atomic. In the process of roughing it out and testing it, I made a small pile of minor but potentially breaking changes: - Removed entry_map from settings and hardcoded handling of various types of entry as some if-statements instead. - Removed embedded_perl flag in settings - was always turned on in practice, and wasn't very coherent since templating would have broken without it. - bin/wrt-display - now handles the "feed" alias correctly - EntryStore: now supports retrieving values for properties with prop_value() - this isn't currently used, but it seems like a reasonable extension of the property idea. - Added `wrt ls --with-titles`. - Added dependency versions to Build.PL. - Refactored Markup's line_parse() a little. - Refactored some tests to give cleaner / more useful output. - Renamed default template file to "default".
4 years ago
cache rendered html; extract titles; all as utf-8 This stashes the HTML version of every entry in memory and uses Mojo::DOM to extract headers from the markup for use as titles. Titles are displayed in $self->{page_navigation}, now available inside templates as ${page_navigation}. In order to keep Mojo::DOM from choking on other input, it uses the open pragma to open everything as UTF-8 by default, which also eliminates a whole class of character encoding bugs and removes some fiddling with Encode::decode() from feed_print(). This is all obviously a more memory-intensive, but caching the markup turns out to have the side effect of making it much faster to render even a large site, probably as much as anything because the HTML in question is only getting generated once per entry instead of (potentially) 2-3 times. This commit isn't very atomic. In the process of roughing it out and testing it, I made a small pile of minor but potentially breaking changes: - Removed entry_map from settings and hardcoded handling of various types of entry as some if-statements instead. - Removed embedded_perl flag in settings - was always turned on in practice, and wasn't very coherent since templating would have broken without it. - bin/wrt-display - now handles the "feed" alias correctly - EntryStore: now supports retrieving values for properties with prop_value() - this isn't currently used, but it seems like a reasonable extension of the property idea. - Added `wrt ls --with-titles`. - Added dependency versions to Build.PL. - Refactored Markup's line_parse() a little. - Refactored some tests to give cleaner / more useful output. - Renamed default template file to "default".
4 years ago
cache rendered html; extract titles; all as utf-8 This stashes the HTML version of every entry in memory and uses Mojo::DOM to extract headers from the markup for use as titles. Titles are displayed in $self->{page_navigation}, now available inside templates as ${page_navigation}. In order to keep Mojo::DOM from choking on other input, it uses the open pragma to open everything as UTF-8 by default, which also eliminates a whole class of character encoding bugs and removes some fiddling with Encode::decode() from feed_print(). This is all obviously a more memory-intensive, but caching the markup turns out to have the side effect of making it much faster to render even a large site, probably as much as anything because the HTML in question is only getting generated once per entry instead of (potentially) 2-3 times. This commit isn't very atomic. In the process of roughing it out and testing it, I made a small pile of minor but potentially breaking changes: - Removed entry_map from settings and hardcoded handling of various types of entry as some if-statements instead. - Removed embedded_perl flag in settings - was always turned on in practice, and wasn't very coherent since templating would have broken without it. - bin/wrt-display - now handles the "feed" alias correctly - EntryStore: now supports retrieving values for properties with prop_value() - this isn't currently used, but it seems like a reasonable extension of the property idea. - Added `wrt ls --with-titles`. - Added dependency versions to Build.PL. - Refactored Markup's line_parse() a little. - Refactored some tests to give cleaner / more useful output. - Renamed default template file to "default".
4 years ago
cache rendered html; extract titles; all as utf-8 This stashes the HTML version of every entry in memory and uses Mojo::DOM to extract headers from the markup for use as titles. Titles are displayed in $self->{page_navigation}, now available inside templates as ${page_navigation}. In order to keep Mojo::DOM from choking on other input, it uses the open pragma to open everything as UTF-8 by default, which also eliminates a whole class of character encoding bugs and removes some fiddling with Encode::decode() from feed_print(). This is all obviously a more memory-intensive, but caching the markup turns out to have the side effect of making it much faster to render even a large site, probably as much as anything because the HTML in question is only getting generated once per entry instead of (potentially) 2-3 times. This commit isn't very atomic. In the process of roughing it out and testing it, I made a small pile of minor but potentially breaking changes: - Removed entry_map from settings and hardcoded handling of various types of entry as some if-statements instead. - Removed embedded_perl flag in settings - was always turned on in practice, and wasn't very coherent since templating would have broken without it. - bin/wrt-display - now handles the "feed" alias correctly - EntryStore: now supports retrieving values for properties with prop_value() - this isn't currently used, but it seems like a reasonable extension of the property idea. - Added `wrt ls --with-titles`. - Added dependency versions to Build.PL. - Refactored Markup's line_parse() a little. - Refactored some tests to give cleaner / more useful output. - Renamed default template file to "default".
4 years ago
cache rendered html; extract titles; all as utf-8 This stashes the HTML version of every entry in memory and uses Mojo::DOM to extract headers from the markup for use as titles. Titles are displayed in $self->{page_navigation}, now available inside templates as ${page_navigation}. In order to keep Mojo::DOM from choking on other input, it uses the open pragma to open everything as UTF-8 by default, which also eliminates a whole class of character encoding bugs and removes some fiddling with Encode::decode() from feed_print(). This is all obviously a more memory-intensive, but caching the markup turns out to have the side effect of making it much faster to render even a large site, probably as much as anything because the HTML in question is only getting generated once per entry instead of (potentially) 2-3 times. This commit isn't very atomic. In the process of roughing it out and testing it, I made a small pile of minor but potentially breaking changes: - Removed entry_map from settings and hardcoded handling of various types of entry as some if-statements instead. - Removed embedded_perl flag in settings - was always turned on in practice, and wasn't very coherent since templating would have broken without it. - bin/wrt-display - now handles the "feed" alias correctly - EntryStore: now supports retrieving values for properties with prop_value() - this isn't currently used, but it seems like a reasonable extension of the property idea. - Added `wrt ls --with-titles`. - Added dependency versions to Build.PL. - Refactored Markup's line_parse() a little. - Refactored some tests to give cleaner / more useful output. - Renamed default template file to "default".
4 years ago
cache rendered html; extract titles; all as utf-8 This stashes the HTML version of every entry in memory and uses Mojo::DOM to extract headers from the markup for use as titles. Titles are displayed in $self->{page_navigation}, now available inside templates as ${page_navigation}. In order to keep Mojo::DOM from choking on other input, it uses the open pragma to open everything as UTF-8 by default, which also eliminates a whole class of character encoding bugs and removes some fiddling with Encode::decode() from feed_print(). This is all obviously a more memory-intensive, but caching the markup turns out to have the side effect of making it much faster to render even a large site, probably as much as anything because the HTML in question is only getting generated once per entry instead of (potentially) 2-3 times. This commit isn't very atomic. In the process of roughing it out and testing it, I made a small pile of minor but potentially breaking changes: - Removed entry_map from settings and hardcoded handling of various types of entry as some if-statements instead. - Removed embedded_perl flag in settings - was always turned on in practice, and wasn't very coherent since templating would have broken without it. - bin/wrt-display - now handles the "feed" alias correctly - EntryStore: now supports retrieving values for properties with prop_value() - this isn't currently used, but it seems like a reasonable extension of the property idea. - Added `wrt ls --with-titles`. - Added dependency versions to Build.PL. - Refactored Markup's line_parse() a little. - Refactored some tests to give cleaner / more useful output. - Renamed default template file to "default".
4 years ago
cache rendered html; extract titles; all as utf-8 This stashes the HTML version of every entry in memory and uses Mojo::DOM to extract headers from the markup for use as titles. Titles are displayed in $self->{page_navigation}, now available inside templates as ${page_navigation}. In order to keep Mojo::DOM from choking on other input, it uses the open pragma to open everything as UTF-8 by default, which also eliminates a whole class of character encoding bugs and removes some fiddling with Encode::decode() from feed_print(). This is all obviously a more memory-intensive, but caching the markup turns out to have the side effect of making it much faster to render even a large site, probably as much as anything because the HTML in question is only getting generated once per entry instead of (potentially) 2-3 times. This commit isn't very atomic. In the process of roughing it out and testing it, I made a small pile of minor but potentially breaking changes: - Removed entry_map from settings and hardcoded handling of various types of entry as some if-statements instead. - Removed embedded_perl flag in settings - was always turned on in practice, and wasn't very coherent since templating would have broken without it. - bin/wrt-display - now handles the "feed" alias correctly - EntryStore: now supports retrieving values for properties with prop_value() - this isn't currently used, but it seems like a reasonable extension of the property idea. - Added `wrt ls --with-titles`. - Added dependency versions to Build.PL. - Refactored Markup's line_parse() a little. - Refactored some tests to give cleaner / more useful output. - Renamed default template file to "default".
4 years ago
cache rendered html; extract titles; all as utf-8 This stashes the HTML version of every entry in memory and uses Mojo::DOM to extract headers from the markup for use as titles. Titles are displayed in $self->{page_navigation}, now available inside templates as ${page_navigation}. In order to keep Mojo::DOM from choking on other input, it uses the open pragma to open everything as UTF-8 by default, which also eliminates a whole class of character encoding bugs and removes some fiddling with Encode::decode() from feed_print(). This is all obviously a more memory-intensive, but caching the markup turns out to have the side effect of making it much faster to render even a large site, probably as much as anything because the HTML in question is only getting generated once per entry instead of (potentially) 2-3 times. This commit isn't very atomic. In the process of roughing it out and testing it, I made a small pile of minor but potentially breaking changes: - Removed entry_map from settings and hardcoded handling of various types of entry as some if-statements instead. - Removed embedded_perl flag in settings - was always turned on in practice, and wasn't very coherent since templating would have broken without it. - bin/wrt-display - now handles the "feed" alias correctly - EntryStore: now supports retrieving values for properties with prop_value() - this isn't currently used, but it seems like a reasonable extension of the property idea. - Added `wrt ls --with-titles`. - Added dependency versions to Build.PL. - Refactored Markup's line_parse() a little. - Refactored some tests to give cleaner / more useful output. - Renamed default template file to "default".
4 years ago
cache rendered html; extract titles; all as utf-8 This stashes the HTML version of every entry in memory and uses Mojo::DOM to extract headers from the markup for use as titles. Titles are displayed in $self->{page_navigation}, now available inside templates as ${page_navigation}. In order to keep Mojo::DOM from choking on other input, it uses the open pragma to open everything as UTF-8 by default, which also eliminates a whole class of character encoding bugs and removes some fiddling with Encode::decode() from feed_print(). This is all obviously a more memory-intensive, but caching the markup turns out to have the side effect of making it much faster to render even a large site, probably as much as anything because the HTML in question is only getting generated once per entry instead of (potentially) 2-3 times. This commit isn't very atomic. In the process of roughing it out and testing it, I made a small pile of minor but potentially breaking changes: - Removed entry_map from settings and hardcoded handling of various types of entry as some if-statements instead. - Removed embedded_perl flag in settings - was always turned on in practice, and wasn't very coherent since templating would have broken without it. - bin/wrt-display - now handles the "feed" alias correctly - EntryStore: now supports retrieving values for properties with prop_value() - this isn't currently used, but it seems like a reasonable extension of the property idea. - Added `wrt ls --with-titles`. - Added dependency versions to Build.PL. - Refactored Markup's line_parse() a little. - Refactored some tests to give cleaner / more useful output. - Renamed default template file to "default".
4 years ago
cache rendered html; extract titles; all as utf-8 This stashes the HTML version of every entry in memory and uses Mojo::DOM to extract headers from the markup for use as titles. Titles are displayed in $self->{page_navigation}, now available inside templates as ${page_navigation}. In order to keep Mojo::DOM from choking on other input, it uses the open pragma to open everything as UTF-8 by default, which also eliminates a whole class of character encoding bugs and removes some fiddling with Encode::decode() from feed_print(). This is all obviously a more memory-intensive, but caching the markup turns out to have the side effect of making it much faster to render even a large site, probably as much as anything because the HTML in question is only getting generated once per entry instead of (potentially) 2-3 times. This commit isn't very atomic. In the process of roughing it out and testing it, I made a small pile of minor but potentially breaking changes: - Removed entry_map from settings and hardcoded handling of various types of entry as some if-statements instead. - Removed embedded_perl flag in settings - was always turned on in practice, and wasn't very coherent since templating would have broken without it. - bin/wrt-display - now handles the "feed" alias correctly - EntryStore: now supports retrieving values for properties with prop_value() - this isn't currently used, but it seems like a reasonable extension of the property idea. - Added `wrt ls --with-titles`. - Added dependency versions to Build.PL. - Refactored Markup's line_parse() a little. - Refactored some tests to give cleaner / more useful output. - Renamed default template file to "default".
4 years ago
cache rendered html; extract titles; all as utf-8 This stashes the HTML version of every entry in memory and uses Mojo::DOM to extract headers from the markup for use as titles. Titles are displayed in $self->{page_navigation}, now available inside templates as ${page_navigation}. In order to keep Mojo::DOM from choking on other input, it uses the open pragma to open everything as UTF-8 by default, which also eliminates a whole class of character encoding bugs and removes some fiddling with Encode::decode() from feed_print(). This is all obviously a more memory-intensive, but caching the markup turns out to have the side effect of making it much faster to render even a large site, probably as much as anything because the HTML in question is only getting generated once per entry instead of (potentially) 2-3 times. This commit isn't very atomic. In the process of roughing it out and testing it, I made a small pile of minor but potentially breaking changes: - Removed entry_map from settings and hardcoded handling of various types of entry as some if-statements instead. - Removed embedded_perl flag in settings - was always turned on in practice, and wasn't very coherent since templating would have broken without it. - bin/wrt-display - now handles the "feed" alias correctly - EntryStore: now supports retrieving values for properties with prop_value() - this isn't currently used, but it seems like a reasonable extension of the property idea. - Added `wrt ls --with-titles`. - Added dependency versions to Build.PL. - Refactored Markup's line_parse() a little. - Refactored some tests to give cleaner / more useful output. - Renamed default template file to "default".
4 years ago
cache rendered html; extract titles; all as utf-8 This stashes the HTML version of every entry in memory and uses Mojo::DOM to extract headers from the markup for use as titles. Titles are displayed in $self->{page_navigation}, now available inside templates as ${page_navigation}. In order to keep Mojo::DOM from choking on other input, it uses the open pragma to open everything as UTF-8 by default, which also eliminates a whole class of character encoding bugs and removes some fiddling with Encode::decode() from feed_print(). This is all obviously a more memory-intensive, but caching the markup turns out to have the side effect of making it much faster to render even a large site, probably as much as anything because the HTML in question is only getting generated once per entry instead of (potentially) 2-3 times. This commit isn't very atomic. In the process of roughing it out and testing it, I made a small pile of minor but potentially breaking changes: - Removed entry_map from settings and hardcoded handling of various types of entry as some if-statements instead. - Removed embedded_perl flag in settings - was always turned on in practice, and wasn't very coherent since templating would have broken without it. - bin/wrt-display - now handles the "feed" alias correctly - EntryStore: now supports retrieving values for properties with prop_value() - this isn't currently used, but it seems like a reasonable extension of the property idea. - Added `wrt ls --with-titles`. - Added dependency versions to Build.PL. - Refactored Markup's line_parse() a little. - Refactored some tests to give cleaner / more useful output. - Renamed default template file to "default".
4 years ago
12 years ago
  1. package App::WRT::Markup;
  2. use strict;
  3. use warnings;
  4. use feature "state";
  5. use base qw(Exporter);
  6. our @EXPORT_OK = qw(line_parse image_markup eval_perl);
  7. use App::WRT::Image qw(image_size);
  8. use App::WRT::Util qw(file_get_contents);
  9. use Carp;
  10. use File::Basename;
  11. use Text::Textile;
  12. use Text::Markdown::Discount;
  13. # Some useful defaults:
  14. my %tags = (
  15. retcon => q{div class="retcon"},
  16. freeverse => 'p',
  17. list => "ul>\n<li"
  18. );
  19. my %end_tags = (
  20. retcon => 'div',
  21. freeverse => 'p',
  22. list => "li>\n</ul"
  23. );
  24. my %blank_lines = (
  25. freeverse => "</p>\n\n<p>",
  26. list => "</li>\n\n<li>"
  27. );
  28. my %newlines = (
  29. freeverse => "<br />\n"
  30. );
  31. my %dashes = (
  32. freeverse => ' &mdash; '
  33. );
  34. =over
  35. =item eval_perl
  36. Evaluate embedded Perl in a string, replacing blocks enclosed with <perl> tags
  37. with whatever they return (well, evaluated in a scalar context). Returns the
  38. modified string.
  39. Also handles simple ${variables}, replacing them from the keys to $self.
  40. =cut
  41. sub eval_perl {
  42. my $self = shift;
  43. my ($text) = @_;
  44. while ($text =~ m{<perl>(.*?)</perl>}s) {
  45. my $block = $1;
  46. # Run the $block, and include anything returned:
  47. my $output = eval $block;
  48. if ($@) {
  49. # Errors - log and return an empty string:
  50. carp($@);
  51. $output = '';
  52. }
  53. $text =~ s{<perl>\Q$block\E</perl>}{$output}s;
  54. }
  55. # Interpolate variables:
  56. $text =~ s{
  57. \$\{ ([a-zA-Z_]+) \}
  58. }{
  59. if (defined $self->{$1}) {
  60. $self->{$1};
  61. } else {
  62. # TODO: Possibly this should be fatal.
  63. "UNDEFINED: $1";
  64. }
  65. }gex;
  66. return $text;
  67. }
  68. =item line_parse
  69. Performs substitutions on lines called by fragment_slurp, at least. Calls
  70. include_process(), image_markup(), textile_process(), markdown_process(),
  71. eval_perl().
  72. Applies before-parsing and after-parsing filters.
  73. Returns string.
  74. Parses some special markup. Specifically:
  75. <perl>print "hello world";</perl>
  76. ${variable} interpolation from the WRT object
  77. <include>path/to/file/from/project/root</include>
  78. <textile></textile> - Text::Textile to HTML
  79. <markdown></markdown> - Text::Markdown::Discount to HTML
  80. <image>filename.ext
  81. optional alt tag
  82. optional title text</image>
  83. <freeverse></freeverse>
  84. <retcon></retcon>
  85. <list></list>
  86. =cut
  87. sub line_parse {
  88. my $self = shift;
  89. my ($everything, $file) = (@_);
  90. # Eventually, this should probably only happen for templates:
  91. $everything = $self->eval_perl($everything);
  92. # Take care of <include>, <textile>, <markdown>, and <image> tags:
  93. include_process($self, $everything);
  94. textile_process($everything);
  95. markdown_process($everything);
  96. $everything =~ s!<image>(.*?)</image>!$self->image_markup($file, $1)!seg;
  97. foreach my $key (keys %tags) {
  98. # Set some replacements, unless they've been explicitly set already:
  99. $end_tags{$key} ||= $tags{$key};
  100. # Transform blocks:
  101. while ($everything =~ m| (<$key>\n?) (.*?) (\n?</$key>) |sx) {
  102. my $open = $1;
  103. my $block = $2;
  104. my $close = $3;
  105. # Save the bits between instances of the block:
  106. my (@interstices) = split /\Q$open$block$close\E/s, $everything;
  107. # Transform dashes, blank lines, and newlines:
  108. dashes($dashes{$key}, $block) if defined $dashes{$key};
  109. $block =~ s/\n\n/$blank_lines{$key}/gs if defined $blank_lines{$key};
  110. newlines($newlines{$key}, $block) if defined $newlines{$key};
  111. # Slap it all back together as $everything, with start and end
  112. # tags:
  113. $block = "<$tags{$key}>$block</$end_tags{$key}>";
  114. $everything = join $block, @interstices;
  115. }
  116. }
  117. return $everything;
  118. }
  119. =item newlines($replacement, $block)
  120. Inline replace single newlines (i.e., line ends) within the block, except those
  121. preceded by a double-quote, which probably indicates a still-open tag.
  122. =cut
  123. sub newlines {
  124. $_[1] =~ s/(?<=[^"\n]) # not a double-quote or newline
  125. # don't capture
  126. \n # end-of-line
  127. (?=[^\n]) # not a newline
  128. # don't capture
  129. /$_[0]/xgs;
  130. }
  131. =item dashes($replacement, $block)
  132. Inline replace double dashes in a block - " -- " - with a given replacement.
  133. =cut
  134. sub dashes {
  135. $_[1] =~ s/(\s+) # whitespace - no capture
  136. \-{2} # two dashes
  137. (\n|\s+|$) # newline, whitespace, or eol
  138. /$1$_[0]$2/xgs;
  139. }
  140. =item include_process
  141. Inline replace <include>filename</include> tags, replacing them with the
  142. contents of files.
  143. =cut
  144. sub include_process {
  145. my $wrt = shift;
  146. $_[0] =~ s{
  147. <include> # start tag
  148. (.*?) # anything (non-greedy)
  149. </include> # end tag
  150. }{
  151. retrieve_include($wrt, $1);
  152. }xesg;
  153. }
  154. =item retrieve_include
  155. Get the contents of an included file. This probably needs a great
  156. deal more thought than I am presently giving it.
  157. =cut
  158. sub retrieve_include {
  159. my $wrt = shift;
  160. my ($file) = @_;
  161. # Trim leading and trailing spaces:
  162. $file =~ s/^\s+//;
  163. $file =~ s/\s+$//;
  164. if ($file =~ m{^ (/ | [.]/) }x) {
  165. # TODO: Leads with a slash or a ./
  166. croak('Tried to open an include path with a leading / or ./ - not yet supported.');
  167. } else {
  168. # Use the archive root as path.
  169. $file = $wrt->{root_dir} . '/' . $file;
  170. }
  171. if ($wrt->{cache_includes}) {
  172. if (defined $wrt->{include_cache}->{$file}) {
  173. return $wrt->{include_cache}->{$file};
  174. }
  175. }
  176. unless (-e $file) {
  177. carp "No such file: $file";
  178. return '';
  179. }
  180. if (-d $file) {
  181. carp("Tried to open a directory as an include path: $file");
  182. return '';
  183. }
  184. if ($wrt->{cache_includes}) {
  185. $wrt->{include_cache}->{$file} = file_get_contents($file);
  186. return $wrt->{include_cache}->{$file};
  187. } else {
  188. return file_get_contents($file);
  189. }
  190. }
  191. =item textile_process
  192. Inline replace <textile> markup in a string.
  193. =cut
  194. # This is exactly the kind of code that, even though it isn't doing anything
  195. # especially over the top, looks ghastly to people who don't read Perl, so I'll
  196. # try to explain a bit.
  197. sub textile_process {
  198. # First, there's a state variable here which can retain the Text::Textile
  199. # object between invocations of the function, saving us a bit of time on
  200. # subsequent calls. This should be equivalent to creating a closure around
  201. # the function and keeping a $textile variable there.
  202. state $textile;
  203. # Second, instead of unrolling the arguments to the function, we just act
  204. # directly on the first (0th) one. =~ more or less means "do a regexy
  205. # thing on this". It's followed by s, the substitution operator, which can
  206. # use curly braces as delimiters between pattern and replacement.
  207. $_[0] =~ s{
  208. # Find tags...
  209. <textile> # start tag
  210. (.*?) # anything (non-greedy)
  211. </textile> # end tag
  212. }{
  213. # ...and replace them with the result of evaluating this block.
  214. # //= means "defined-or-equals"; if the var hasn't been defined yet,
  215. # then make a new Textile object:
  216. $textile //= Text::Textile->new();
  217. # Process the stuff we slurped out of our tags - this value will be
  218. # used to replace the entire match from above (in Perl, the last
  219. # expression evaluated is the return value of subs, evals, etc.):
  220. $textile->process($1);
  221. }xesg;
  222. # x: eXtended regexp - whitespace ignored by default, comments allowed
  223. # e: Execute the replacement as Perl code, and use its value
  224. # s: treat all lines of the search subject as a Single string
  225. # g: Globally replace all matches
  226. # For the genuinely concise version of this, see markdown_process().
  227. }
  228. =item markdown_process
  229. Inline replace <markdown> markup in a string.
  230. =cut
  231. sub markdown_process {
  232. state $markdown;
  233. my $flags = Text::Markdown::Discount::MKD_EXTRA_FOOTNOTE();
  234. $_[0] =~ s{
  235. <markdown>(.*?)</markdown>
  236. }{
  237. $markdown //= Text::Markdown::Discount->new;
  238. $markdown->markdown($1, $flags);
  239. }xesg;
  240. }
  241. =item image_markup
  242. Parse out an image tag and return the appropriate html.
  243. Relies on image_size from App::WRT::Image.
  244. =cut
  245. sub image_markup {
  246. my $self = shift;
  247. my ($file, $block) = @_;
  248. # Get a basename and directory for the file (entry) referencing the image:
  249. my ($basename, $dir) = fileparse($file);
  250. # Truncated file date that just includes date + sub docs:
  251. my ($file_date) = $dir =~ m{
  252. (
  253. [0-9]{4}/ # year
  254. [0-9]{1,2}/ # month
  255. [0-9]{1,2}/ # day
  256. ([a-z]*/)* # sub-entries
  257. )
  258. $
  259. }x;
  260. # Process the contents of the <image> tag:
  261. my ($image_url, $alt_text, $title_text) = split /\n/, $block;
  262. $alt_text ||= q{};
  263. $title_text ||= $alt_text;
  264. # Resolve relative paths:
  265. my $image_file;
  266. if (-e "$dir/$image_url" ) {
  267. # The path is to an image file in the same directory as current entry:
  268. $image_file = "$dir/$image_url";
  269. $image_url = "${file_date}${image_url}";
  270. } elsif (-e $self->{entry_dir} . "/$image_url") {
  271. # The path is to an image file starting with the entry_dir, like
  272. # 2005/9/20/candles.jpg -> ./archives/2005/9/20/candles.jpg
  273. $image_file = $self->{entry_dir} . "/$image_url";
  274. }
  275. # Get width & height in pixels for known filetypes:
  276. my ($width, $height) = image_size($self->{root_dir_abs} . '/' . $image_file);
  277. # This probably relies on mod_rewrite working:
  278. $image_url = $self->{image_url_root} . $image_url;
  279. return <<"IMG";
  280. <img src="$image_url"
  281. width="$width"
  282. height="$height"
  283. alt="$alt_text"
  284. title="$title_text" />
  285. IMG
  286. }
  287. =back
  288. 1;