-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.php
More file actions
90 lines (82 loc) · 3.1 KB
/
Copy pathcli.php
File metadata and controls
90 lines (82 loc) · 3.1 KB
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
<?php
use dokuwiki\plugin\totext\Extractor\ExtractorFactory;
use splitbrain\phpcli\Options;
/**
* DokuWiki Plugin totext (CLI Component)
*
* Prints the plain text extracted from a document to STDOUT.
*
* @license GPL-2.0-only
* @author Andreas Gohr <gohr@cosmocode.de>
*/
class cli_plugin_totext extends \dokuwiki\Extension\CLIPlugin
{
/** @inheritDoc */
protected function setup(Options $options)
{
$options->setHelp(
'Extract a document to STDOUT. By default prints the body text ' .
'followed by the metadata as "Key: value" lines. ' .
'Supported formats: ' . implode(', ', ExtractorFactory::supportedExtensions())
);
$options->registerOption(
'text',
'Print only the body text (omit metadata).',
't'
);
$options->registerOption(
'meta',
'Print only the metadata as "Key: value" lines (omit body text).',
'm'
);
$options->registerArgument('file', 'The file to extract from', true);
}
/** @inheritDoc */
protected function main(Options $options)
{
[$file] = $options->getArgs();
// Any ExtractionException bubbles up; the phpcli CLI base catches it,
// prints the message to STDERR and exits non-zero.
$result = ExtractorFactory::extract($file);
// --text and --meta are mutually exclusive "only this" switches; with
// neither (the default) both are shown, metadata after the body text.
$showText = !$options->getOpt('meta');
$showMeta = !$options->getOpt('text');
if (!$showText && !$showMeta) {
$showText = $showMeta = true;
}
// A requested half may have failed while the other was salvaged.
$failures = [];
if ($showText && $result->textError !== null) {
$failures['text'] = $result->textError;
}
if ($showMeta && $result->metadataError !== null) {
$failures['metadata'] = $result->metadataError;
}
$blocks = [];
if ($showText && $result->text !== '') {
$blocks[] = $result->text;
}
if ($showMeta && $result->metadata !== []) {
$lines = [];
foreach ($result->metadata as $key => $value) {
$lines[] = "$key: $value";
}
$blocks[] = implode("\n", $lines);
}
// Nothing usable came through for what was requested: a hard failure.
// Re-throwing lets the CLI's exception handler report it and exit
// non-zero (the same path a total failure already takes).
if ($blocks === [] && $failures !== []) {
throw reset($failures);
}
// Partial success: warn on STDERR about the failed half but still emit
// the salvaged half on STDOUT. Body text first, then the metadata block,
// separated by a blank line.
foreach ($failures as $what => $error) {
$this->warning("$what extraction failed: " . $error->getMessage());
}
echo implode("\n\n", $blocks) . "\n";
return 0;
}
}