| 1 | package HTML::FormatText::Any; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | our $DATE = '2019-01-14'; # DATE | |||||||||
| 4 | 1 | our $VERSION = '0.001'; # VERSION | |||||||||
| 5 | |||||||||||
| 6 | 1 | ▶ | use 5.010001; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use Log::ger; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 1 | ▶ | use Exporter qw(import); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 1 | our @EXPORT_OK = qw(html2text); | |||||||||
| 13 | |||||||||||
| 14 | 1 | our %SPEC; | |||||||||
| 15 | |||||||||||
| 16 | $SPEC{html2text} = { | ||||||||||
| 17 | 1 | v => 1.1, | |||||||||
| 18 | summary => 'Render HTML as text using one of multiple backends', | ||||||||||
| 19 | description => <<'_', | ||||||||||
| 20 | |||||||||||
| 21 | Backends are tried in the following order (order is chosen based on rendering | ||||||||||
| 22 | quality): | ||||||||||
| 23 | |||||||||||
| 24 | * <pm:HTML::FormatText::Elinks> (using external program 'elinks') | ||||||||||
| 25 | * <pm:HTML::FormatText::Links> (using external program 'links') | ||||||||||
| 26 | * <pm:HTML::FormatText::W3m> (using external program 'w3m') | ||||||||||
| 27 | * <pm:HTML::FormatText::Lynx> (using external program 'lynx') | ||||||||||
| 28 | * <pm:HTML::FormatText::WithLinks::AndTables> | ||||||||||
| 29 | |||||||||||
| 30 | _ | ||||||||||
| 31 | args => { | ||||||||||
| 32 | html => { | ||||||||||
| 33 | schema => 'str*', | ||||||||||
| 34 | req => 1, | ||||||||||
| 35 | tags => ['category:input'], | ||||||||||
| 36 | pos => 0, | ||||||||||
| 37 | cmdline_src => 'stdin_or_files', | ||||||||||
| 38 | }, | ||||||||||
| 39 | # XXX option to customize order of backends | ||||||||||
| 40 | }, | ||||||||||
| 41 | links => [ | ||||||||||
| 42 | {url => 'prog:html2text', summary => 'CLI for this module'}, | ||||||||||
| 43 | {url => 'prog:html2txt', summary => 'a simpler HTML rendering utility which basically just strips HTML tags from HTML source code'}, | ||||||||||
| 44 | ], | ||||||||||
| 45 | 'cmdline.skip_format' => 1, | ||||||||||
| 46 | }; | ||||||||||
| 47 | 0 | ▶ | sub html2text { | ||||||||
|
Subroutine
html2text: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||
| 48 | 0 | require File::Which; | |||||||||
| 49 | |||||||||||
| 50 | 0 | my %args = @_; | |||||||||
| 51 | 0 | ▶ | my $html = $args{html} or return [400, "Please specify html"]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | |||||||||||
| 53 | ELINKS: | ||||||||||
| 54 | { | ||||||||||
| 55 | 0 | ▶ | last unless File::Which::which("elinks"); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | 0 | log_trace "Trying to render HTML using elinks ..."; | |||||||||
| 57 | 0 | require HTML::FormatText::Elinks; | |||||||||
| 58 | 0 | my $text = HTML::FormatText::Elinks->format_string($html); | |||||||||
| 59 | 0 | ▶ | unless (defined $text) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | 0 | log_trace "Couldn't render using elinks, ". | |||||||||
| 61 | "trying another backend"; | ||||||||||
| 62 | 0 | last; | |||||||||
| 63 | } | ||||||||||
| 64 | 0 | return [200, "OK (elinks)", $text]; | |||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | LINKS: | ||||||||||
| 68 | { | ||||||||||
| 69 | 0 | ▶ | last unless File::Which::which("links"); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 0 | log_trace "Trying to render HTML using links ..."; | |||||||||
| 71 | 0 | require HTML::FormatText::Links; | |||||||||
| 72 | 0 | my $text = HTML::FormatText::Links->format_string($html); | |||||||||
| 73 | 0 | ▶ | unless (defined $text) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | 0 | log_trace "Couldn't render using links, ". | |||||||||
| 75 | "trying another backend"; | ||||||||||
| 76 | 0 | last; | |||||||||
| 77 | } | ||||||||||
| 78 | 0 | return [200, "OK (links)", $text]; | |||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | W3M: | ||||||||||
| 82 | { | ||||||||||
| 83 | 0 | ▶ | last unless File::Which::which("w3m"); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | 0 | log_trace "Trying to render HTML using w3m ..."; | |||||||||
| 85 | 0 | require HTML::FormatText::W3m; | |||||||||
| 86 | 0 | my $text = HTML::FormatText::W3m->format_string($html); | |||||||||
| 87 | 0 | ▶ | unless (defined $text) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 0 | log_trace "Couldn't render using w3m, ". | |||||||||
| 89 | "trying another backend"; | ||||||||||
| 90 | 0 | last; | |||||||||
| 91 | } | ||||||||||
| 92 | 0 | return [200, "OK (w3m)", $text]; | |||||||||
| 93 | } | ||||||||||
| 94 | |||||||||||
| 95 | LYNX: | ||||||||||
| 96 | { | ||||||||||
| 97 | 0 | ▶ | last unless File::Which::which("lynx"); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | 0 | log_trace "Trying to render HTML using lynx ..."; | |||||||||
| 99 | 0 | require HTML::FormatText::Lynx; | |||||||||
| 100 | 0 | my $text = HTML::FormatText::Lynx->format_string($html); | |||||||||
| 101 | 0 | ▶ | unless (defined $text) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 102 | 0 | log_trace "Couldn't render using lynx, ". | |||||||||
| 103 | "trying another backend"; | ||||||||||
| 104 | 0 | last; | |||||||||
| 105 | } | ||||||||||
| 106 | 0 | return [200, "OK (lynx)", $text]; | |||||||||
| 107 | } | ||||||||||
| 108 | |||||||||||
| 109 | # fallback | ||||||||||
| 110 | 0 | log_trace "Rendering HTML using HTML::FormatText::WithLinks::AndTables ..."; | |||||||||
| 111 | 0 | require HTML::FormatText::WithLinks::AndTables; | |||||||||
| 112 | 0 | my $text = HTML::FormatText::WithLinks::AndTables->convert($html); | |||||||||
| 113 | 0 | [200, "OK (HTML::FormatText::WithLinks::AndTables)", $text]; | |||||||||
| 114 | } | ||||||||||
| 115 | |||||||||||
| 116 | 1 | 1; | |||||||||
| 117 | # ABSTRACT: Render HTML as text using one of multiple backends | ||||||||||
| 118 | |||||||||||
| 119 | __END__ | ||||||||||