-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.php
97 lines (76 loc) · 1.98 KB
/
import.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
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
<?php
$file = file_get_contents("import.file");
$file = explode("===", $file);
array_pop($file);
foreach ($file as $key => $value) {
$key . PHP_EOL;
$key . PHP_EOL;
$key . PHP_EOL;
echo PHP_EOL;
echo "====";
echo PHP_EOL;
$value = preg_replace('/^[ \t]*[\r\n]+/m', '', $value);
/*
layout: post
title: Welcome to LogChimp
date: 2018-08-11 23:59:59 +0530
category: New
author: Your Name
*/
$v = explode("---", $value);
$meta = trim($v[0]);
if(strpos($meta, "layout:") === false) {
$meta = "layout: post".PHP_EOL.$meta;
}
if(strpos($meta, "author:") === false) {
$meta = $meta.PHP_EOL."author: Markus";
}
$meta = "---".PHP_EOL.trim($meta).PHP_EOL."---".PHP_EOL;
$metaarray = explode(PHP_EOL, $meta);
foreach ($metaarray as $mk => $mv) {
if(strpos($mv, "date:") !== false) {
$date = explode(": ", $mv)[1];
$date = explode(" ", $date)[0];
}
if(strpos($mv, "title:") !== false) {
$title = explode(": ", $mv)[1];
}
}
$body = $v[1];
$body = str_replace("<strong>", "**", $body);
$body = str_replace("</strong>", "**", $body);
$body = str_replace("<code>", "`", $body);
$body = str_replace("</code>", "`", $body);
$body = str_replace("<p>", "", $body);
$body = str_replace("</p>", " ".PHP_EOL, $body);
$filename = $date . "-" . slugify($title) . ".md";
//$body = strip_tags($v[1]);
$text = "";
$text .= $meta;
$text .= PHP_EOL;
$text .= trim($body);
//$text .= PHP_EOL;
//$text .= "====";
//$text .= PHP_EOL;
file_put_contents("_posts/".$filename, $text);
}
//print_r($file);
function slugify($text)
{
// replace non letter or digits by -
$text = preg_replace('~[^\pL\d]+~u', '-', $text);
// transliterate
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
// remove unwanted characters
$text = preg_replace('~[^-\w]+~', '', $text);
// trim
$text = trim($text, '-');
// remove duplicate -
$text = preg_replace('~-+~', '-', $text);
// lowercase
$text = strtolower($text);
if (empty($text)) {
return 'n-a';
}
return $text;
}