Converter::processMarkdown()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
crap 2
1
<?php
2
3
namespace tomzx\GoogleDocsToMarkdown;
4
5
use League\HTMLToMarkdown\HtmlConverter;
6
use tomzx\GoogleDocsToMarkdown\HtmlConverter\EmphasisConverter;
7
8
class Converter {
9
	/**
10
	 * @var \League\HTMLToMarkdown\HtmlConverter
11
	 */
12
	protected $converter;
13
14
	public function __construct()
15
	{
16
		$this->converter = new HtmlConverter([
17
			'header_style' => 'atx',
18
			'strip_tags' => true,
19
			'remove_nodes' => 'head',
20
		]);
21
	}
22
23
	/**
24
	 * @return \League\HTMLToMarkdown\HtmlConverter
25
	 */
26
	public function getConverter()
27
	{
28
		return $this->converter;
29
	}
30
31
	/**
32
	 * @param string $html
33
	 * @return string
34
     */
35
	public function convert($html)
36
	{
37
		$markdown = $this->converter->convert($html);
38
		return $this->processMarkdown($markdown);
39
	}
40
41
	/**
42
	 * @param string $markdown
43
	 * @return string
44
     */
45
	protected function processMarkdown($markdown)
46
	{
47
		// Remove Google link tracking
48
		$markdown = preg_replace_callback('/\(https?:\/\/www\.google\.com\/url\?q=(?<url>[^&]+)[^\)]+\)/', function ($matches) {
49
			return '('.urldecode($matches['url']).')';
50
		}, $markdown);
51
		return $markdown;
52
	}
53
}
54