-
Notifications
You must be signed in to change notification settings - Fork 73
/
convert-markdown.ini
125 lines (102 loc) · 3.62 KB
/
convert-markdown.ini
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
[Command]
Name=Convert Markdown to ...
Command="
copyq:
// # get input text to be converted
var markdown = str(input());
if (!markdown) {
copy();
markdown = clipboard();
}
// # get conversion options from user
var renderers = {
'HTML': 'mistletoe.html_renderer.HTMLRenderer',
'HTML + code highlighting': 'mistletoe.contrib.pygments_renderer.PygmentsRenderer', // requires: `pip3 install pygments`
'HTML + GitHub Wiki': 'mistletoe.contrib.github_wiki.GithubWikiRenderer',
'HTML + MathJax': 'mistletoe.contrib.mathjax.MathJaxRenderer',
'Jira': 'mistletoe.contrib.jira_renderer.JIRARenderer',
'JSON (AST)': 'mistletoe.ast_renderer.ASTRenderer',
'LaTeX': 'mistletoe.latex_renderer.LaTeXRenderer',
'XWiki Syntax 2.0': 'mistletoe.contrib.xwiki20_renderer.XWiki20Renderer',
}
var settingsPrefix = 'convert-markdown/';
var optFormat = 'Target format';
var optAddToHistory = 'Add result to clipboard history';
var optHtmlAsSourceOnly = 'Output HTML as source code only';
var format = settings(settingsPrefix + optFormat);
var addToHistory = settings(settingsPrefix + optAddToHistory) == 'true';
var htmlAsSourceOnly = settings(settingsPrefix + optHtmlAsSourceOnly) == 'true';
var options = dialog(
'.title', 'Convert Markdown to ...',
'.defaultChoice', format,
optFormat, Object.keys(renderers),
optAddToHistory, addToHistory,
optHtmlAsSourceOnly, htmlAsSourceOnly
);
if (!options) {
abort();
}
// # parse and store the options
format = options[optFormat];
addToHistory = options[optAddToHistory];
htmlAsSourceOnly = options[optHtmlAsSourceOnly];
settings(settingsPrefix + optFormat, format);
settings(settingsPrefix + optAddToHistory, addToHistory);
settings(settingsPrefix + optHtmlAsSourceOnly, htmlAsSourceOnly);
// # do the conversion
function tempFile(content) {
var file = new TemporaryFile();
file.openWriteOnly();
file.write(content);
file.close();
return file;
}
var mdFile = tempFile(markdown);
var cmdRes = execute('python', '-m', 'mistletoe', mdFile.fileName(), '--renderer', renderers[format]);
if (!cmdRes || cmdRes.exit_code != 0) {
popup('', 'Conversion failed: ' + (cmdRes ? str(cmdRes.stderr) : 'Python executable is probably not available?'), -1);
fail();
}
// # store conversion result
var output = str(cmdRes.stdout);
function html2text(html) {
// strip tags
var text = html.replace(/<[^>]*>?/gm, '');
// replace known entities
text = text.replace(/&([^;]+);/g, function (match, p1) {
var chars = {
'lt': '<',
'gt': '>',
'amp': '&',
'#39': '\\'',
'nbsp': '\\xa0',
}
return chars[p1] || p1;
});
return text;
}
var item = {};
if (htmlAsSourceOnly || format.indexOf('HTML') == -1) {
item[mimeText] = output;
} else {
item[mimeHtml] = output;
item[mimeText] = html2text(output);
}
function copyItem(item) {
args = [];
for (prop in item) {
args.push(prop, item[prop]);
}
// copy() signature: copy(mimeType, data, [mimeType, data]...)
copy.apply(this, args);
}
if (addToHistory) {
add(item);
}
copyItem(item);
popup('', 'Markdown successfully converted to ' + format + '!');
"
InMenu=true
IsGlobalShortcut=true
Icon=\xf103
GlobalShortcut=meta+alt+c