forked from aleron75/mageres
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv2md.php
101 lines (80 loc) · 3.06 KB
/
csv2md.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
98
99
100
101
<?php
const NO_SUBTOPIC = '0__no_subtopic__';
if (!isset($argv[2])) {
echo "Usage: php csv2md.php input_file output_file.csv" . PHP_EOL;
exit(1);
}
$csv = $argv[1];
if (!file_exists($csv)) {
echo "File not found: $csv" . PHP_EOL;
exit(1);
}
$outfile = $argv[2];
if ($csv == $outfile) {
echo "Cannot write on input file!" . PHP_EOL;
exit(1);
}
$countents = [];
$content = '';
$topic = '';
$prevtopic = '';
$subtopic = '';
$prevsubtopic = '';
$resourcesNumber = 0;
if (($handle = fopen($csv, "r")) !== false) {
while (($data = fgetcsv($handle, 1000, ",")) !== false) {
list($topic, $subtopic, $name, $url, $description) = $data;
if (!isset($contents[$topic])) {
$contents[$topic] = [];
}
if (empty($subtopic)) {
$subtopic = NO_SUBTOPIC;
}
if (!isset($contents[$topic][$subtopic])) {
$contents[$topic][$subtopic] = [];
}
$contents[$topic][$subtopic][$name] = [
'url' => $url,
'description' => $description,
];
$resourcesNumber ++;
}
fclose($handle);
}
$toc = '';
foreach ($contents as $topic => $subtopics)
{
$toc .= sprintf("* [%s](#%s)", $topic, str_replace(' ', '-', preg_replace('/[^a-z ]/', '', strtolower($topic)))) . PHP_EOL;
$content .= sprintf(PHP_EOL . "## %s" . PHP_EOL, $topic);
ksort($subtopics);
foreach ($subtopics as $subtopic => $subtopicdata) {
$content .= PHP_EOL;
if (NO_SUBTOPIC != $subtopic) {
$content .= sprintf("### %s" . PHP_EOL . PHP_EOL, $subtopic);
}
uksort($subtopicdata, 'strnatcasecmp');
foreach ($subtopicdata as $name => $data) {
$content .= sprintf("* [%s](%s)%s%s" . PHP_EOL, $name, $data['url'], empty($data['description']) ? '' : ' - ', $data['description']);
}
}
}
$introduction = <<<OUT
# Magento Resources [![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](CONTRIBUTING.md) [![$resourcesNumber resources](https://img.shields.io/badge/resources-$resourcesNumber-orange.svg?style=flat)](#table-of-contents) [![Build Status](https://api.travis-ci.com/aleron75/mageres.svg?branch=master)](https://travis-ci.com/aleron75/mageres)
<p align="center">
<img src="https://raw.githubusercontent.com/aleron75/mageres/master/media/mageres.png" alt="mageres logo"/>
</p>
A curated list of **useful Magento resources**.
Resources are listed **alphabetically** within each category.
This file is automatically generated from the [resources.csv](resources.csv) file using the following command:
`php csv2md.php resources.csv README.md`
If you want to contribute, consider updating the `resources.csv` and regenerating the `README.md` file instead of manually changing it.
## Stay up to date!
If you want to **stay up to date with changes**, you can [subscribe to the monthly digest](https://mailchi.mp/6a498018d9ef/mageres).
OUT;
$toc = PHP_EOL . '## Table of Contents' . PHP_EOL . PHP_EOL . $toc;
$out = $introduction . $toc . $content;
$fp = fopen($outfile, 'w');
fwrite($fp, $out);
fclose($fp);
#echo $out;
#print_r($contents);