| 1 | package Galileo::Command::dump; | ||
| 2 | use Mojo::Base 'Mojolicious::Command'; | ||
| 3 | use File::Spec; | ||
| 4 | use Mojo::Util qw/encode spurt/; | ||
| 5 | use Getopt::Long qw/GetOptionsFromArray/; | ||
| 6 | |||
| 7 | has description => "Dump all stored pages as markdown\n"; | ||
| 8 | has usage => <<END; | ||
| 9 | usage: $0 dump [options] | ||
| 10 | options: | ||
| 11 | |||
| 12 | --directory,-d | ||
| 13 | An optional directory to dump to may be specified; the directory | ||
| 14 | must exist. By default it dumps to the current working directory. | ||
| 15 | |||
| 16 | --title,-t | ||
| 17 | By default the title of the page is included as an HTML comment. | ||
| 18 | This option accepts an sprintf format for including the title. | ||
| 19 | As a special case, if this flag is given without argument, an h1 | ||
| 20 | title is created. | ||
| 21 | |||
| 22 | --encoding,-e | ||
| 23 | An encoding type. Defaults to UTF-8. Available encodings are the | ||
| 24 | same as Encode module of Perl. | ||
| 25 | END | ||
| 26 | |||
| 27 | sub run { | ||
| 28 | my $self = shift; | ||
| 29 | |||
| 30 | GetOptionsFromArray( \@_, | ||
| 31 | 'directory=s' => \my $dir, | ||
| 32 | 'title:s' => \(my $title = '<!-- %s -->'), | ||
| 33 | 'encoding:s' => \(my $encoding = 'UTF-8'), | ||
| 34 | ); | ||
| 35 | |||
| 36 | $title = '# %s' unless $title; | ||
| 37 | if ( $dir and ! -d $dir ) { | ||
| 38 | die qq{Directory "$dir" does not exist\n}; | ||
| 39 | } | ||
| 40 | |||
| 41 | my $pages = $self->app->schema->resultset('Page')->search; | ||
| 42 | while ( my $page = $pages->next ) { | ||
| 43 | my $file = $page->name . '.md'; | ||
| 44 | $file = File::Spec->catfile( $dir, $file ) if $dir; | ||
| 45 | |||
| 46 | my $content = sprintf "$title\n", $page->title; | ||
| 47 | $content .= $page->md; | ||
| 48 | $content = encode($encoding, $content) if $encoding; | ||
| 49 | spurt $content, $file; | ||
| 50 | } | ||
| 51 | |||
| 52 | say "Export Complete"; | ||
| 53 | } | ||
| 54 | |||
| 55 | 1; | ||
| 56 | |||
| 57 |