This repository has been archived by the owner on Dec 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.php
227 lines (204 loc) · 8.85 KB
/
index.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
declare(strict_types = 1);
namespace Avonture;
/*
* AUTHOR : AVONTURE Christophe
*
* Written date : 3 october 2018
*
* CSV to markdown converter.
* Need to be rework for adding a form where we can specify the input string,
* delimiters (like "," or ";"), click on a submit button and, thanks to an ajax
* request, get the result in a dynamic div; without leaving the form.
*
* Last mod:
* 2019-01-01 - Abandonment of jQuery and migration to vue.js
*/
define('REPO', 'https://github.com/cavo789/marknotes_csv2md');
require_once 'classes/Csv2Md.php';
// Retrieve posted data
$data = json_decode(file_get_contents('php://input'), true);
if ($data !== []) {
$task = filter_var(($data['task'] ?? ''), FILTER_SANITIZE_STRING);
if ('convert' == $task) {
// Retrieve the CSV content
$csv = base64_decode(filter_var(($data['csv'] ?? ''), FILTER_SANITIZE_STRING));
// Delimiters between columns (, or ; or ...)
$delim = base64_decode($data['delim'] ?? '');
if ('' == trim($delim)) {
$delim = ',';
}
// In case of the text are between quotes like, for instance,
// "field1","field2", ... If so, mention '"' as value for $enclosure
$enclosure = base64_decode($data['enclosure'] ?? '');
// Separator to use in markdown to separate columns ('|' is the standard)
$separator = base64_decode($data['separator'] ?? '');
if ('' == trim($separator)) {
$separator = '|';
}
// Transpose will works ONLY when there is two records in the
// input string. Instead of having a long "horizontal" table, convert
// the table vertically. The result table will have two columns and as
// many rows that there was columns in the string
$bTranspose = boolval(filter_var(($data['transpose'] ?? ''), FILTER_VALIDATE_BOOLEAN));
// Create a new CSV parser
$csvParser = new Csv2Md();
$csvParser->setCsv($csv);
$csvParser->setDelimiter($delim);
$csvParser->setEnclosure($enclosure);
$csvParser->setTableSeparator($separator);
$csvParser->setTranspose($bTranspose);
// Create a Markdown table from the parsed input
header('Content-Type: text/plain');
echo base64_encode($csvParser->getMarkup());
die();
}
}
// Get the GitHub corner
$github = '';
if (is_file($cat = __DIR__ . DIRECTORY_SEPARATOR . 'octocat.tmpl')) {
$github = str_replace('%REPO%', REPO, file_get_contents($cat));
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="author" content="Christophe Avonture" />
<meta name="robots" content="noindex, nofollow" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=9; IE=8;" />
<title>Marknotes - CSV2MD</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
</head>
<body>
<?php echo $github; ?>
<div class="container">
<div class="page-header">
<h1>Marknotes - CSV2MD</h1>
</div>
<div class="container" id="app">
<div class="form-group">
<how-to-use demo="https://raw.githubusercontent.com/cavo789/marknotes_csv2md/master/images/demo.gif">
<ul>
<li>Copy/Paste your CSV content in the textbox below</li>
<li>Update one or more options</li>
<li>If you've only two lines, you can select Transpose</li>
<li>Click on the Convert button</li>
</ul>
</how-to-use>
<label for="csv">Copy/Paste your CSV content in the textbox below then click on the Convert button:</label>
<textarea class="form-control" rows="5" v-model="CSV" name="csv" @paste="doChangeTabByComma"></textarea>
</div>
<div class="row">
<form class="form-inline">
<div class="form-check mr-sm-3">
<input type="checkbox" class="form-check-input" v-model="transpose">
<label class="form-check-label" for="transpose">Transpose</label>
</div>
<div class=" form-group mr-sm-3">
<label for="delim">Delimiter:</label>
<input type="text" style="width:50px;" size="3" maxlength="3" v-model="delim" class="form-control">
</div>
<div class=" form-group mr-sm-3">
<label for="enclosure">Quote:</label>
<input type="text" style="width:50px;" size="3" maxlength="3" v-model="enclosure" class="form-control">
</div>
<div class=" form-group mr-sm-3">
<label for="separator">Separator:</label>
<input type="text" style="width:50px;" size="3" maxlength="3" v-model="separator" class="form-control">
</div>
</form>
</div>
<button type="button" @click="doConvert" class="btn btn-primary">Convert</button>
<hr />
<div v-if="Markdown!==''">
<h2 id="markdown">Markdown code <small style="font-size:0.4em"><a href="#html">See HTML rendering</a></small></h2>
<pre v-html="Markdown"></pre>
<hr />
</div>
<div v-if="HTML!==''">
<h2 id="html">HTML rendering <small style="font-size:0.4em"><a href="#markdown">See Markdown code</a></small></h2>
<pre v-html="HTML"></pre>
<hr />
</div>
</div>
</div>
<script src="https://unpkg.com/vue@2"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="https://unpkg.com/marked@0.3.6"></script>
<script type="text/javascript">
Vue.component('how-to-use', {
props: {
demo: {
type: String,
required: true
}
},
template: `<details>
<summary>How to use?</summary>
<div class="row">
<div class="col-sm">
<slot></slot>
</div>
<div class="col-sm"><img v-bind:src="demo" alt="Demo"></div>
</div>
</div>
</details>`
});
var app = new Vue({
el: '#app',
data: {
CSV: "Column 1 Header,Column 2 Header\n" +
"Row 1-1,Row 1-2\n" +
"Row 2-1,Row 2-2",
transpose: false,
delim: ',',
enclosure: '"',
separator: '|',
Markdown: ''
},
methods: {
doConvert() {
var $data = {
task: 'convert',
csv: window.btoa(this.CSV),
transpose: this.transpose,
delim: window.btoa(this.delim),
enclosure: window.btoa(this.enclosure),
separator: window.btoa(this.separator)
}
axios.post('<?php echo basename(__FILE__); ?>', $data)
.then(response => (this.Markdown = window.atob(response.data)))
.catch(function(error) {
console.log(error);
});
},
doChangeTabByComma(event) {
// By copying/pasting from Excel or SSMS (SQL Server Management Studio),
// replace tabs by comma so the conversion is easier.
this.CSV = event.clipboardData.getData('Text');
this.CSV = this.CSV.replace(/ /g, ',')
event.clipboardData.setData('text/plain', this.CSV);
event.preventDefault();
}
},
computed: {
HTML() {
if (this.Markdown == '') {
return '';
}
// Call marked() to convert the MD string into a HTML table
var mdTable = marked(this.Markdown, {
sanitize: true
});
// Add Boostrap classes
mdTable = mdTable.replace('<table>', '<table class="table table-hover table-striped">');
return mdTable;
}
}
});
</script>
</body>
</html>