-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathconversion_functions.js
221 lines (209 loc) · 7.87 KB
/
conversion_functions.js
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
function columnWidth(rows, columnIndex) {
return Math.max.apply(null, rows.map(function(row) {
return row[columnIndex].length
}))
}
function split_to_rows(data) {
return data.split((/[\n\u0085\u2028\u2029]|\r\n?/g)).map(function(row) {
row = row.replace('\n', ' ')
return row.split("\t")
})
}
function convert_to_md(data) {
var rows=split_to_rows(data)
var colAlignments = []
var columnWidths = rows[0].map(function(column, columnIndex) {
var alignment = "l"
var re = /^(\^[lcr])/i
var m = column.match(re)
if (m) {
var align = m[1][1].toLowerCase()
if (align === "c") {
alignment = "c"
} else if (align === "r") {
alignment = "r"
}
}
colAlignments.push(alignment)
column = column.replace(re, "")
rows[0][columnIndex] = column
return columnWidth(rows, columnIndex)
})
var markdownRows = rows.map(function(row, rowIndex) {
// | Name | Title | Email Address |
// |--------------|-------|----------------|
// | Jane Atler | CEO | [email protected] |
// | John Doherty | CTO | [email protected] |
// | Sally Smith | CFO | [email protected] |
return "| " + row.map(function(column, index) {
return column + Array(columnWidths[index] - column.length + 1).join(" ")
}).join(" | ") + " |"
row.map
})
markdownRows.splice(1, 0, "|" + columnWidths.map(function(width, index) {
var prefix = ""
var postfix = ""
var adjust = 0
var alignment = colAlignments[index]
if (alignment === "r") {
postfix = ":"
adjust = 1
} else if (alignment == "c") {
prefix = ":"
postfix = ":"
adjust = 2
}
return prefix + Array(columnWidths[index] + 3 - adjust).join("-") + postfix
}).join("|") + "|")
return markdownRows.join("\n")
}
function convert_to_listtable(data) {
var rows=split_to_rows(data)
// remove the unwanted characters that have no effect in restructured text
var firstRow=rows[0].map(function(column,columnIndex){
var re = /^(\^[lcr])/i
return(column.replace(re, ""))
})
rows[0]=firstRow
// Run through it row by row and for each row column by column
// The first column has a different indent
// Each cell has its own line
var rstListTableRows = rows.map(function(row, rowIndex) {
return row.map(function(column, index) {
var indentString = ( index > 0 ) ? "\t - " : "\t* - "
return indentString + column
}).join("\n")
})
var columnWidths = ""
if(rows[0].length>1) {
var columnWidth = Math.floor(100 / rows[0].length)
var firstColumnWidth = 100 - columnWidth*(rows[0].length-1)
columnWidths = rows[0].map(function(column,index) {
return "" + ( index > 0 ) ? columnWidth : firstColumnWidth
}).join(" ")
} else {
columnWidths = "" + 100
}
return ".. list-table:: Title"+"\n" + "\t:widths: " + columnWidths + "\n" + "\t:header-rows: 1" + "\n\n" + rstListTableRows.join("\n")
}
function convert_to_variant1(data) {
var rows=split_to_rows(data)
var firstRow=rows[0].map(function(column,columnIndex){
var re = /^(\^[lcr])/i
return(column.replace(re, ""))
})
rows[0]=firstRow
var columnWidths = rows[0].map(function(column, columnIndex) {
return columnWidth(rows, columnIndex)
})
var indentString = "\t"
// Get the table
// Run through it row by row and for each row column by column
// The first column has a different indent
// Each cell has its own line
// The indent is based on 4 spaces, which seems to be quite standard in python
// +======+=====+========+==========+
// | naam | int | letter | fraction |
// +======+=====+========+==========+
// | aap | 1 | a | 0.333333 |
// +------+-----+--------+----------+
// | noot | 2 | b | 0.250000 |
// +------+-----+--------+----------+
// | mies | 3 | c | 0.200000 |
// +======+=====+========+==========+
var rstTableRows = rows.map(function(row, rowIndex) {
return "| " + row.map(function(column, index) {
return column + Array(columnWidths[index] - column.length + 1).join(" ")
}).join(" | ") + " |"
})
var lenSize = rstTableRows.length
if(lenSize>1) {
rstTableRows.splice(1, 0, "+" + columnWidths.map(function(width, index) {
return Array(columnWidths[index] + 3).join("=")
}).join("+") + "+")
} else {
// if there is only one row, then is the last one
rstTableRows.splice(1, 0, "+" + columnWidths.map(function(width, index) {
return Array(columnWidths[index] + 3).join("-")
}).join("+") + "+")
}
rstTableRows.splice(0, 0, "+" + columnWidths.map(function(width, index) {
return Array(columnWidths[index] + 3).join("-")
}).join("+") + "+")
lenSize = rstTableRows.length
if( lenSize > 3 ) {
// the last row
rstTableRows.splice(lenSize, 0, "+" + columnWidths.map(function(width, index) {
return Array(columnWidths[index] + 3).join("-")
}).join("+") + "+")
// Now we need to look whether we have more than 5, if so then we need to do the
// normal table inserts
lenSize = rstTableRows.length
if(lenSize > 5) {
var lastRow = lenSize - 1
for(i=lastRow-1;i>3;i=i-1) {
rstTableRows.splice(i, 0, "+" + columnWidths.map(function(width, index) {
return Array(columnWidths[index] + 3).join("-")
}).join("+") + "+")
}
}
}
// https://www.w3.org/TR/clipboard-apis/#the-paste-action
// When pasting, the drag data store mode flag is read-only, hence calling
// setData() from a paste event handler will not modify the data that is
// inserted, and not modify the data on the clipboard.
// In other words, we cannot already reset the clipboard from here, leaving it to the user to manually copy
// the text in the text area
// Some say this need the htmlonly directive ... but rst2html does not know about it
return rstTableRows.map(function(row,rowIndex) { return indentString + row }).join("\n")
}
function convert_to_variant2(data) {
var rows=split_to_rows(data)
var firstRow=rows[0].map(function(column,columnIndex){
var re = /^(\^[lcr])/i
return(column.replace(re, ""))
})
rows[0]=firstRow
var columnWidths = rows[0].map(function(column, columnIndex) {
return columnWidth(rows, columnIndex)
})
var indentString = "\t"
// Run through it row by row and for each row column by column
// The first column has a different indent
// Each cell has its own line
// The indent is based on 4 spaces, which seems to be quite standard in python
// .. htmlonly::
//
var rstTableRows = rows.map(function(row, rowIndex) {
return row.map(function(column, index) {
if( columnWidths[index]-column.length +1 > 0 ) {
return column + Array(columnWidths[index] - column.length + 1 ).join(" ")
} else {
return column
}
}).join(" ")
})
// x
// underline open
rstTableRows.splice(1, 0, columnWidths.map(function(width, index) {
return Array(columnWidths[index]+1).join("=")
}).join(" ") )
// first
rstTableRows.splice(0, 0, columnWidths.map(function(width, index) {
return Array(columnWidths[index] +1).join("=")
}).join(" ") )
var lenSize = rstTableRows.length
if( lenSize > 3 ) {
rstTableRows.splice(lenSize, 0, columnWidths.map(function(width, index) {
return Array(columnWidths[index] +1).join("=")
}).join(" ") )
}
return rstTableRows.map(function(row,rowIndex) { return indentString + row }).join("\n")
}
// the below line adds nothing, it just serves to make the mocha testing work
// from the directory this file is in simply run `mocha` (if you have it installed)
// to perform the tests
exports.convert_to_md=convert_to_md;
exports.convert_to_listtable=convert_to_listtable;
exports.convert_to_variant1=convert_to_variant1;
exports.convert_to_variant2=convert_to_variant2;