aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/league/html-to-markdown/bin/html-to-markdown
blob: 2815a7cef504af75f69a8bde5d8d3c07c0fcc60f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env php
<?php

requireAutoloader();

ini_set('display_errors', 'stderr');

foreach ($argv as $i => $arg) {
    if ($i === 0) {
        continue;
    }

    if (substr($arg, 0, 1) === '-') {
        switch ($arg) {
            case '-h':
            case '--help':
                echo getHelpText();
                exit(0);
            default:
                fail('Unknown option: ' . $arg);
        }
    } else {
        $src = $argv[1];
    }
}

if (isset($src)) {
    if (!file_exists($src)) {
        fail('File not found: ' . $src);
    }

    $html = file_get_contents($src);
} else {
    $stdin = fopen('php://stdin', 'r');
    stream_set_blocking($stdin, false);
    $html = stream_get_contents($stdin);
    fclose($stdin);

    if (empty($html)) {
        fail(getHelpText());
    }
}


$converter = new League\HTMLToMarkdown\HtmlConverter();
echo $converter->convert($html);

/**
 * Get help and usage info
 *
 * @return string
 */
function getHelpText()
{
    return <<<HELP
HTML To Markdown

Usage: html-to-markdown [OPTIONS] [FILE]

    -h, --help  Shows help and usage information

    If no file is given, input will be read from STDIN

Examples:

    Converting a file named document.html:

        html-to-markdown document.html

    Converting a file and saving its output:

        html-to-markdown document.html > output.md

    Converting from STDIN:

        echo -e '<h1>Hello World!</h1>' | html-to-markdown

    Converting from STDIN and saving the output:

        echo -e '<h1>Hello World!</h1>' | html-to-markdown > output.md

HELP;
}

/**
 * @param string $message Error message
 */
function fail($message)
{
    fwrite(STDERR, $message . "\n");
    exit(1);
}

function requireAutoloader()
{
    $autoloadPaths = array(
        // Local package usage
        __DIR__ . '/../vendor/autoload.php',
        // Package was included as a library
        __DIR__ . '/../../../autoload.php',
    );
    foreach ($autoloadPaths as $path) {
        if (file_exists($path)) {
            require_once $path;
            break;
        }
    }
}