This macro makes easy to paste on documents (MS Office, Open Office, iWorks Pages) code snippets with syntax highlight and/or line numbers.
This macro copies the selected code text or whole document if no selection is present on clipboard in HTML format.
On OSX you need at least Komodo 6 because previous versions don't support data flavors
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 | var view = ko.views.manager.currentView;
var tmpFileSvc = Components.classes["@activestate.com/koFileService;1"]
.getService(Components.interfaces.koIFileService)
fname = tmpFileSvc.makeTempName(".html");
var lang = view.document.languageObj;
var forceColor = true;
var selectionOnly = view.selection != "";
var schemeService = Components.classes['@activestate.com/koScintillaSchemeService;1'].getService();
schemeService.convertToHTMLFile(view.scimoz,
view.document.displayPath,
view.document.language,
lang.styleBits,
view.document.encoding.python_encoding_name,
fname,
selectionOnly,
forceColor);
var file = Components.classes["@activestate.com/koFileEx;1"]
.createInstance(Components.interfaces.koIFileEx)
file.URI = ko.uriparse.localPathToURI(fname);
file.open('rb');
var str = file.readfile();
file.close();
str = str.replace(/\n|\r/g, "");
var m = str.match(/\s*[a-zA-Z0-9._-]*\s*{.*?}/g);
var styles = {};
for (var i in m) {
var selector = m[i].match(/\s*([a-zA-Z0-9._-]*)\s*{(.*?)}/);
if (selector && /^span\./.test(selector)) {
styles[selector[1].substring("span.".length)] = selector[2].replace(/(^\s*|\s*$)/g, "");
}
}
m = str.match(/(<span.*?)<\/body>/);
var result = m[1]
for (i in styles) {
var style = styles[i];
result = result.replace(new RegExp('<span class="' + i + '">', "g"), '<span style="' + style + '">');
}
result = result.replace(/ /g, ' ') // OpenOffice Writer ignores nbsp
//.replace(/<br ?\/>/g, '');
result = '<pre><span style="font-size:12px">' + result + '</span></pre>';
xtk.include("clipboard");
transferable = xtk.clipboard.addTextDataFlavor("text/html", result);
xtk.clipboard.copyFromTransferable(transferable);
|
Thank you, thank you, thank you. It's been irritating me for a while that I can copy code from dev studio into word and still have it readable, but not from Komodo. Good job, this has just made my boring documentation job that little bit more bearable :-) Steve B
I'm using a 'dark' theme, so I changed the
pre
wrapper to include a dark background (also got rid of the span).Is there any way to get the code without line #s? Turning off line numbers from application menu : view>line numbers makes no difference.
Hi Dennis,
you can turn off line numbers disabling 'print line numbers' from printing category preference
Komodo 7+ requires all instances of "view.document" to be changed to "view.koDoc". With this change, your macro works perfectly in Komodo Edit 8.5!
Thanks for sharing!