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.

103 lines
2.0 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
  1. package App::WRT::Util;
  2. use strict;
  3. use warnings;
  4. use open qw(:std :utf8);
  5. use Carp;
  6. use Encode;
  7. use base qw(Exporter);
  8. our @EXPORT_OK = qw(dir_list file_put_contents file_get_contents);
  9. =over
  10. =item dir_list($dir, $sort_order, $pattern)
  11. Return a $sort_order sorted list of files matching regex $pattern in a
  12. directory.
  13. Calls $sort_order, which can be one of:
  14. alpha - alphabetical
  15. reverse_alpha - alphabetical, reversed
  16. high_to_low - numeric, high to low
  17. low_to_high - numeric, low to high
  18. =cut
  19. sub dir_list {
  20. my ($dir, $sort_order, $pattern) = @_;
  21. $pattern //= qr/^[0-9]{1,2}$/;
  22. $sort_order //= 'high_to_low';
  23. opendir my $list_dir, $dir
  24. or croak "Couldn't open $dir: $!";
  25. my @files = sort $sort_order
  26. grep { m/$pattern/ }
  27. readdir $list_dir;
  28. closedir $list_dir;
  29. return @files;
  30. }
  31. # Various named sorts for dir_list:
  32. sub alpha { $a cmp $b; } # alphabetical
  33. sub high_to_low { $b <=> $a; } # numeric, high to low
  34. sub low_to_high { $a <=> $b; } # numberic, low to high
  35. sub reverse_alpha { $b cmp $a; } # alphabetical, reversed
  36. =item file_put_contents($file, $contents)
  37. Write $contents string to $file path. Because:
  38. L<https://secure.php.net/manual/en/function.file-put-contents.php>
  39. =cut
  40. sub file_put_contents {
  41. my ($file, $contents) = @_;
  42. open(my $fh, '>', $file)
  43. or croak "Unable to open $file for writing: $!";
  44. print $fh $contents;
  45. close $fh;
  46. }
  47. =item file_get_contents($file)
  48. Get contents string of $file path. Because:
  49. L<https://secure.php.net/manual/en/function.file-get-contents.php>
  50. =cut
  51. sub file_get_contents {
  52. my ($file) = @_;
  53. # Make warnings here fatal, and return some useful info about which file is
  54. # being opened:
  55. local $SIG{__WARN__} = sub {
  56. croak "$_[0] when opening $file\n";
  57. };
  58. open my $fh, '<', $file
  59. or croak "Couldn't open $file: $!\n";
  60. my $contents;
  61. {
  62. # line separator:
  63. local $/ = undef;
  64. $contents = <$fh>;
  65. }
  66. close $fh or croak "Couldn't close $file: $!";
  67. return $contents;
  68. }
  69. =back
  70. 1;