]> git.mxchange.org Git - friendica.git/blob - vendor/league/html-to-markdown/bin/html-to-markdown
Move HTML to Markdown library to Composer
[friendica.git] / vendor / league / html-to-markdown / bin / html-to-markdown
1 #!/usr/bin/env php
2 <?php
3
4 requireAutoloader();
5
6 ini_set('display_errors', 'stderr');
7
8 foreach ($argv as $i => $arg) {
9     if ($i === 0) {
10         continue;
11     }
12
13     if (substr($arg, 0, 1) === '-') {
14         switch ($arg) {
15             case '-h':
16             case '--help':
17                 echo getHelpText();
18                 exit(0);
19             default:
20                 fail('Unknown option: ' . $arg);
21         }
22     } else {
23         $src = $argv[1];
24     }
25 }
26
27 if (isset($src)) {
28     if (!file_exists($src)) {
29         fail('File not found: ' . $src);
30     }
31
32     $html = file_get_contents($src);
33 } else {
34     $stdin = fopen('php://stdin', 'r');
35     stream_set_blocking($stdin, false);
36     $html = stream_get_contents($stdin);
37     fclose($stdin);
38
39     if (empty($html)) {
40         fail(getHelpText());
41     }
42 }
43
44
45 $converter = new League\HTMLToMarkdown\HtmlConverter();
46 echo $converter->convert($html);
47
48 /**
49  * Get help and usage info
50  *
51  * @return string
52  */
53 function getHelpText()
54 {
55     return <<<HELP
56 HTML To Markdown
57
58 Usage: html-to-markdown [OPTIONS] [FILE]
59
60     -h, --help  Shows help and usage information
61
62     If no file is given, input will be read from STDIN
63
64 Examples:
65
66     Converting a file named document.html:
67
68         html-to-markdown document.html
69
70     Converting a file and saving its output:
71
72         html-to-markdown document.html > output.md
73
74     Converting from STDIN:
75
76         echo -e '<h1>Hello World!</h1>' | html-to-markdown
77
78     Converting from STDIN and saving the output:
79
80         echo -e '<h1>Hello World!</h1>' | html-to-markdown > output.md
81
82 HELP;
83 }
84
85 /**
86  * @param string $message Error message
87  */
88 function fail($message)
89 {
90     fwrite(STDERR, $message . "\n");
91     exit(1);
92 }
93
94 function requireAutoloader()
95 {
96     $autoloadPaths = array(
97         // Local package usage
98         __DIR__ . '/../vendor/autoload.php',
99         // Package was included as a library
100         __DIR__ . '/../../../autoload.php',
101     );
102     foreach ($autoloadPaths as $path) {
103         if (file_exists($path)) {
104             require_once $path;
105             break;
106         }
107     }
108 }