Skip to content

Commit 07f3dd6

Browse files
committed
Add html:eml conversion
1 parent 120c207 commit 07f3dd6

File tree

4 files changed

+122
-23
lines changed

4 files changed

+122
-23
lines changed

README.md

+23-22
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,26 @@ This table shows the number of converters configured by default between file ext
1212

1313
fileconverter list extension-table
1414

15-
source | dbk | docx | epub | epub3 | fb2 | html | jpg | man | md | odt | opml | pdf | ps | rtf | txt
16-
--- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | ---
17-
bib | | | | | | | | | | | | 1 | | |
18-
dbk | 1 | 1 | 1 | 1 | 1 | 1 | | 1 | 1 | 1 | 1 | 1 | | 1 | 1
19-
doc | | | | | | | | | | | | 1 | | | 1
20-
html | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 9 | | 1 | 1
21-
jpg | | | | | | | 1 | | | | | | | |
22-
ltx | | | | | | | | | | | | 1 | | |
23-
md | 1 | 1 | 1 | 1 | 1 | 1 | | 1 | 1 | 1 | 1 | 1 | | 1 | 1
24-
opml | 1 | 1 | 1 | 1 | 1 | 1 | | 1 | 1 | 1 | 1 | 1 | | 1 | 1
25-
pdb | | | | | | | | | | | | 1 | | |
26-
pdf | | | | | | | 1 | | | | | 1 | | |
27-
ps | | | | | | | | | | | | 1 | | |
28-
psw | | | | | | | | | | | | 1 | | |
29-
rst | 1 | 1 | 1 | 1 | 1 | 1 | | 1 | 1 | 1 | 1 | 1 | | 1 | 1
30-
rtf | | | | | | | | | | | | 4 | 2 | |
31-
sdw | | | | | | | | | | | | 1 | | |
32-
sxw | | | | | | | | | | | | 1 | | |
33-
tex | 1 | 1 | 1 | 1 | 1 | 1 | | 1 | 1 | 1 | 1 | 1 | | 1 | 1
34-
textile | 1 | 1 | 1 | 1 | 1 | 1 | | 1 | 1 | 1 | 1 | 1 | | 1 | 1
35-
txt | 1 | 1 | 1 | 1 | 1 | 1 | | 1 | 1 | 1 | 1 | 2 | | 1 | 1
36-
vor | | | | | | | | | | | | 1 | | |
15+
source | dbk | docx | eml | epub | epub3 | fb2 | html | jpg | man | md | odt | opml | pdf | pdf/grayscale | ps | rtf | txt
16+
--- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | ---
17+
bib | | | | | | | | | | | | | 1 | | | |
18+
dbk | 1 | 1 | | 1 | 1 | 1 | 1 | | 1 | 1 | 1 | 1 | 1 | | | 1 | 1
19+
doc | | | | | | | | | | | | | 1 | | | | 1
20+
html | 1 | 1 | 1 | 1 | 1 | 1 | 3 | 1 | 1 | 1 | 1 | 1 | 9 | | | 1 | 1
21+
jpg | | | | | | | | 1 | | | | | | | | |
22+
ltx | | | | | | | | | | | | | 1 | | | |
23+
md | 1 | 1 | | 1 | 1 | 1 | 1 | | 1 | 1 | 1 | 1 | 1 | | | 1 | 1
24+
opml | 1 | 1 | | 1 | 1 | 1 | 1 | | 1 | 1 | 1 | 1 | 1 | | | 1 | 1
25+
pdb | | | | | | | | | | | | | 1 | | | |
26+
pdf | | | | | | | | 1 | | | | | 1 | 1 | | |
27+
ps | | | | | | | | | | | | | 1 | | | |
28+
psw | | | | | | | | | | | | | 1 | | | |
29+
rst | 1 | 1 | | 1 | 1 | 1 | 1 | | 1 | 1 | 1 | 1 | 1 | | | 1 | 1
30+
rtf | | | | | | | | | | | | | 4 | | 2 | |
31+
sdw | | | | | | | | | | | | | 1 | | | |
32+
sxw | | | | | | | | | | | | | 1 | | | |
33+
tex | 1 | 1 | | 1 | 1 | 1 | 1 | | 1 | 1 | 1 | 1 | 1 | | | 1 | 1
34+
textile | 1 | 1 | | 1 | 1 | 1 | 1 | | 1 | 1 | 1 | 1 | 1 | | | 1 | 1
35+
txt | 1 | 1 | | 1 | 1 | 1 | 1 | | 1 | 1 | 1 | 1 | 2 | | | 1 | 1
36+
vor | | | | | | | | | | | | | 1 | | | |
37+
wiki | 1 | 1 | | 1 | 1 | 1 | 2 | | 1 | 2 | 1 | 1 | 1 | | | 1 | 1

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"php":">=5.4.5",
2121
"symfony/console":"2.4.*",
2222
"wittiws/htmlawed":"dev-master",
23-
"wittiws/phpquery":"dev-master"
23+
"wittiws/phpquery":"dev-master",
24+
"zendframework/zend-mail":"2.7.*"
2425
},
2526
"autoload":{
2627
"psr-4":{

src/Configuration/ConfigurationDefaults.php

+5
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ public function __construct(&$settings) {
6161
'#engine' => 'Convert\\Catdoc',
6262
),
6363
),
64+
'html->eml' => array(
65+
'nativearchive:default' => array(
66+
'#engine' => 'Convert\\NativeArchive',
67+
),
68+
),
6469
'html->pdf' => array(
6570
'htmldoc:default' => array(
6671
'#engine' => 'Convert\\Htmldoc',

src/Engine/Convert/NativeArchive.php

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
/*
3+
* This file is part of the FileConverter package.
4+
*
5+
* (c) Greg Payne
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace FileConverter\Engine\Convert;
12+
use FileConverter\Engine\EngineBase;
13+
use Zend\Mail\Message;
14+
use Zend\Mime\Message as MimeMessage;
15+
use Zend\Mime\Mime;
16+
use Zend\Mime\Part as MimePart;
17+
18+
class NativeArchive extends EngineBase {
19+
/**
20+
* @todo use Message::fromString($raw) to convert from eml to other formats
21+
*/
22+
public function convertFile($source, $destination) {
23+
if ($this->conversion[0] === 'html' && $this->conversion[1] === 'eml') {
24+
$parts = array(
25+
'html' => NULL,
26+
);
27+
28+
// Load the HTML body.
29+
$html = file_get_contents($source);
30+
$html = preg_replace_callback('@(?<a>\s+src=")(?<src>[^"]+)(?<b>")@si', function ($matches) use (&$parts) {
31+
// Mangle the src attribute.
32+
$src = $matches['src'];
33+
if (strpos($src, '://') === FALSE && !preg_match('@^/|\.\.@s', $src)) {
34+
if (is_file($src)) {
35+
$cid = 'imageid' . sizeof($parts);
36+
$ext = strtolower(pathinfo($src, PATHINFO_EXTENSION));
37+
if ($ext === 'jpg') {
38+
$ext = 'jpeg';
39+
}
40+
$parts[$cid] = new MimePart(fopen($src, 'r'));
41+
$parts[$cid]->type = "image/$ext; name=$cid.$ext";
42+
$parts[$cid]->encoding = Mime::ENCODING_BASE64;
43+
$parts[$cid]->id = $cid;
44+
$src = "cid:$cid";
45+
}
46+
}
47+
48+
// Rebuild the src attribute.
49+
$ret = $matches['a'] . $src . $matches['b'];
50+
return $ret;
51+
}, $html);
52+
53+
// Build the message.
54+
$parts['html'] = new MimePart($html);
55+
$parts['html']->type = Mime::TYPE_HTML;
56+
$parts['html']->charset = 'utf-8';
57+
$parts['html']->encoding = Mime::ENCODING_QUOTEDPRINTABLE;
58+
$body = new MimeMessage();
59+
$body->setParts(array_values($parts));
60+
61+
// Build the full output.
62+
$message = new Message();
63+
$message->setBody($body);
64+
$output = $message->toString();
65+
file_put_contents($destination, $output);
66+
return $this;
67+
}
68+
69+
throw new \InvalidArgumentException("Unsupported conversion type requested");
70+
}
71+
72+
protected function getHelpInstallation($os, $os_version) {
73+
$help = array(
74+
'title' => 'Native Archives',
75+
);
76+
switch ($os) {
77+
case 'Ubuntu':
78+
$help['os'] = 'confirmed on Ubuntu 16.04';
79+
$help['notes'] = array(
80+
'composer update',
81+
);
82+
return $help;
83+
}
84+
85+
return parent::getHelpInstallation($os, $os_version);
86+
}
87+
88+
public function isAvailable() {
89+
return (class_exists('\Zend\Mail\Message'));
90+
}
91+
92+
}

0 commit comments

Comments
 (0)