-
Notifications
You must be signed in to change notification settings - Fork 0
/
toMarkdown.php
45 lines (35 loc) · 1.84 KB
/
toMarkdown.php
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
<?php
libxml_use_internal_errors(true);
$data = file_get_contents("task.html");
$dom = new DOMDocument();
$dom->loadHtml(mb_convert_encoding($data, 'HTML-ENTITIES', 'UTF-8'));
$finder = new DomXPath($dom);
$desc = $finder->query("//*[@class='day-desc']");
ob_start();
$header = $finder->query(".//h2", $desc->item(0));
$headerText = strip_tags($dom->saveHtml($header->item(0)));
preg_match('/Day ([0-9]+)/i', $headerText, $matches);
$currentDay = $matches[1];
$desc->item(0)->removeChild($header->item(0));
echo "# {$headerText}\n\n";
echo "See [How to run](https://github.com/arxeiss/advent-of-code-2020/#how-to-run) chapter to run this puzzle\n\n";
echo "> :warning: **SPOILER ALERT** :warning: - The code contains solution for the whole task. Try first to solve it **yourself**. :link: https://adventofcode.com/2020/day/{$currentDay}\n\n";
echo "## --- Part 1 ---\n\n";
$content = $dom->saveHtml($desc->item(0)).$dom->saveHtml($desc->item(1));
$content = preg_replace('/^[ \t]+/m', '', $content);
$content = preg_replace_callback('/<pre[^>]*>\s*<code[^>]*>(.*?)<\/code>\s*<\/pre>\s*/is', function ($matches) {
$code = strip_tags(trim($matches[1]));
return "```\n{$code}\n```\n\n";
}, $content);
$content = preg_replace_callback('/<code[^>]*>(.*?)<\/code>/is', function ($matches) {
return '`'.strip_tags(trim($matches[1])).'`';
}, $content);
$content = preg_replace('/<\/?em[^>]*>/i', '**', $content);
$content = preg_replace('/<li[^>]*>(.*?)(<\/li>|\n)\s*/i', "- $1\n", $content);
$content = preg_replace('/<\/(p|ul)>\s*/i', "\n\n", $content);
$content = preg_replace('/<h2[^>]*>(.*?)<\/h2>\s*/i', "## $1\n\n", $content);
$content = str_replace("--- Part Two ---", "--- Part 2 ---", $content);
$content = preg_replace('/\n{3,}/', "\n\n", strip_tags($content."\n"));
echo trim($content);
echo "\n";
file_put_contents("day{$currentDay}/README.md", ob_get_clean());