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.

126 lines
2.9 KiB

  1. #!/usr/bin/env perl
  2. =pod
  3. =head1 NAME
  4. wrt-feed - print an Atom or JSON feed from given wrt entries
  5. =head1 USAGE
  6. # Print most recent entries:
  7. wrt feed
  8. wrt feed --json
  9. # Print entries for a specific month:
  10. wrt feed 2019/11
  11. wrt feed --json 2019/11
  12. =head1 DESCRIPTION
  13. Detailed documentation can be found in the L<App::WRT> man page or at
  14. L<https://code.p1k3.com/gitea/brennen/wrt>.
  15. =head1 LICENSE
  16. wrt is free software; you can redistribute it and/or modify
  17. it under the terms of the GNU General Public License as published by
  18. the Free Software Foundation; either version 2 of the License, or
  19. (at your option) any later version.
  20. =head1 AUTHOR
  21. Brennen Bearnes <code@p1k3.com>
  22. =cut
  23. use 5.14.0;
  24. use strict;
  25. use warnings;
  26. no warnings 'uninitialized';
  27. use utf8;
  28. use open qw(:std :utf8);
  29. # use Data::Dumper;
  30. use App::WRT;
  31. use Carp;
  32. use Getopt::Long qw(GetOptionsFromArray);
  33. use Pod::Usage;
  34. # If invoked directly from the command-line, caller() will return undef.
  35. # Execute main() with a callback to print output directly, and a copy of
  36. # our real @ARGV:
  37. if (not caller()) {
  38. my $output = sub { say @_; };
  39. my $retval = main($output, @ARGV);
  40. exit($retval);
  41. }
  42. sub main {
  43. my ($output, @local_argv) = @_;
  44. # Handle options, including help generated from the POD above. See:
  45. # - http://perldoc.perl.org/Getopt/Long.html#User-defined-subroutines-to-handle-options
  46. # - https://metacpan.org/pod/Pod::Usage
  47. # - http://michael.thegrebs.com/2014/06/08/Pod-Usage/
  48. my $from_stdin = 0;
  49. my $print_json = 0;
  50. my $config_file = 'wrt.json';
  51. GetOptionsFromArray(
  52. \@local_argv,
  53. stdin => \$from_stdin,
  54. json => \$print_json,
  55. 'config=s' => \$config_file,
  56. help => sub { pod2usage(0) },
  57. ) or pod2usage(2);
  58. unless (-e $config_file) {
  59. croak "No wrt config file found. Tried: $config_file";
  60. }
  61. my $w = App::WRT::new_from_file($config_file);
  62. # With --stdin, take names of entries to display from standard input, one line
  63. # per name. Otherwise, take names from arguments if any are given, or fall
  64. # back to most recent entries.
  65. my @to_display = ();
  66. if ($from_stdin) {
  67. while (my $entry = <>) {
  68. chomp($entry);
  69. push @to_display, $entry;
  70. }
  71. } elsif (@local_argv) {
  72. (@to_display) = @local_argv;
  73. }
  74. my (@expanded_to_display) = map { $w->expand_alias($_) } @to_display;
  75. foreach my $entry (@expanded_to_display) {
  76. unless ($w->{entries}->is_extant($entry)) {
  77. # TODO: Better error reporting strategy, print this on stderr:
  78. say("No such entry: $entry");
  79. return 1;
  80. }
  81. }
  82. if (@expanded_to_display) {
  83. if ($print_json) {
  84. $output->( $w->feed_print_json(@expanded_to_display) );
  85. } else {
  86. $output->( $w->feed_print(@expanded_to_display) );
  87. }
  88. } else {
  89. if ($print_json) {
  90. $output->( $w->feed_print_json_recent() );
  91. } else {
  92. $output->( $w->feed_print_recent() );
  93. }
  94. }
  95. return 0;
  96. }
  97. 1;