Sublime Forum

Copy text with highlighted colors

#61

Hey guys, just wanted to chime in with an error I was seeing, and what I did to fix. Python isn’t my strong point at all, so there’s likely something else wrong. But hopefully something will help. :smile: I’m using the last pasted code from above, rather than the github repo’s code. When I press ctrl-alt-n, I get an error:

Traceback (most recent call last): File ".\sublime_plugin.py", line 356, in run_ return self.run(edit, **args) File ".\PrintHtml.py", line 18, in run File ".\xml\dom\minidom.py", line 1918, in parse File ".\xml\dom\expatbuilder.py", line 922, in parse IOError: [Errno 22] invalid mode ('rb') or filename: u'D:\\MyStuff\\Dropbox\\Software\\Sublime Text 2\\Data\\PackagesD:\\MyStuff\\Dropbox\\Software\\Sublime Text 2\\Data\\\\Themes - Light\\Colorized-704864249585081-Espresso Soda.tmTheme'
You can see the path to the theme is very crazy. I changed this:

doc = minidom.parse(path_packages + colour_scheme)

To this:

doc = minidom.parse(settings.get('color_scheme'))

And it works fine… I’m running in an irregular setup though. I’m using the USB version of ST2, and you can see in my path that I keep it stored in my Dropbox. So I don’t have the install in its usual location… not sure if that’s part of the problem or not. Oh, and I also created new locations for light and dark themes (as you can see in the path) to help my theme organization.

Hopefully something here helps you out… I’d love for this to be working 100%. :smile: Lastly, would it be possible to add a keybind for copying the html to the clipboard?

0 Likes

#62

[pre=#0C1021]from plist import parse_plist[/pre]

0 Likes

#63

Yeah, the USB version of should be able to use plist as I understand, and honestly, it would be easier to use since it just dumps everything as a dict.

0 Likes

#64

Hmmm.

from plist import parse_plist

did not seem to work on windows. I did use

from plistlib import readPlist

And that worked just fine.

I have my own personal branch that is using the plistlib parser here if you want to try it. @agibsonsw feel free to poach whatever you want from this branch or if you want a pull request, I will create one. I use this at work, so I added the things I need to make it functional for me.

-Don’t create HTMLs everywhere
-Auto-open html in browser
-Display file name and path at the top of the HTML output
-Reduce code complexity (easier to read and maintain the code)
-Alternative print color scheme option
-Use plist parser
-Slight formatting fixes to ensure entire code output can be selected and copied from browser.

github.com/facelessuser/PrintHtml

0 Likes

#65
from plist import parse_plist

:laughing: didn’t know about this :laughing: But, hey, I’ve learnt a lot in meantime.

@**facelessuser **You’ve obviously got a lot of free time on your hands :smiley:. I shall look at your code, and continue to modify mine. I want to fix minor issues, tidy the code, and I want to examine the **plist **module. But I’m still tempted to leave preview in browser as a separate feature, and I’m not too concerned about the proliferation of html. So yours is now more suitable as a package.

At the outset it seemed like a big task, and I hadn’t decided whether I was going to pursue it. But it only took a day, so I’m quite pleased to have been, let’s say… an inspiration :sunglasses:. [Perhaps I can be a footnote to your plugin! You could store a scope in your dictionary called ‘AndyG.parse.overlord’ :wink: ]

Andy.

0 Likes

#66

[quote=“agibsonsw”]from plist import parse_plist

:laughing: didn’t know about this :laughing: But, hey, I’ve learnt a lot in meantime.

@**facelessuser **You’ve obviously got a lot of free time on your hands :smiley:. I shall look at your code, and continue to modify mine. I want to fix minor issues, tidy the code, and I want to examine the **plist **module. But I’m still tempted to leave preview in browser as a separate feature, and I’m not too concerned about the proliferation of html. So yours is now more suitable as a package.

At the outset it seemed like a big task, and I hadn’t decided whether I was going to pursue it. But it only took a day, so I’m quite pleased to have been, let’s say… an inspiration :sunglasses:. [Perhaps I can be a footnote to your plugin! You could store a scope in your dictionary called ‘AndyG.parse.overlord’ :wink: ]

Andy.[/quote]

I wasn’t planning on releasing it. I was just throwing something together that worked for me for work. I am always emailing code snippets to people asking them about their groups code etc. I fully look at this as your plugin; the algorithm is all yours. All I did was fix some issues and some support for what I wanted. I really cannot put my name on this code, it is all you :smile:.

If you want me to release it as a package, I will be more than happy, but credits are yours entirely. I would be the footnote :wink:. It was more for my own purposes that I forked yours; this is so handy for me.

If you want to release the plugin yourself, I want to give you that opportunity. Let me know what you want to do.

0 Likes

#67

Hi @facelessuser. Yes, I still recognise most of my code within yours :smile:. I won’t have much time to develop mine until Wednesday - I’ll probably use that great app WinMerge. I want to examine the regex code your posted yesterday - does it get rid of all those empty spans? I haven’t had a chance to run it yet.

Didn’t realise the follow code would be so simple:

# Open in web browser desktop.open(the_html.name)
Couldn’t this last step be another setting? Not everyone might want to preview. However, the code would have to back-track a little, so that it saves the file for the user. But, again, this could be decided later. Actually, you’re probably right: if their going to save it as HTML then they probably want to preview straight-away, and then they have control over saving it :smile:

If putting the filename at the top of the HTML then it could, perhaps be formatted as a heading. But, as this is just code, I don’t suppose most people would mind/care. However, it might be worth considering following it with a date-stamp(?).

I’ll consider releasing it as a plug-in after Wednesday. I’ll probably need some guidance :smile: :wink:

[The possibilities are endless… Commands could be created to add comments via the input-window - these could be embedded as css/js mouseover events (just kidding :wink: )]

0 Likes

#68

@facelessuser

 m = re.match("^(.*)\r?\n((\r?\n)+)$", tidied_text) if not self.numbers else None

I assume the inclusion of ‘\r’ is for Linux users as it doesn’t register on my Windows system? (Character 13 in old money :wink: )

Is “… if not this else this” new-ish syntax? Almost like a ternary operator.

# Get general theme colors from color scheme file if "background" in colour_settings: self.bground = colour_settings"background"].strip() elif 'foreground' in colour_settings: self.bground = colour_settings"foreground"].strip() elif 'gutterForeground' in colour_settings: self.bground = colour_settings"gutterForeground"].strip()
Shouldn’t these be separate if statements.

How do I update my code to match yours? Is it a pull request?

0 Likes

#69

[quote=“agibsonsw”]

 m = re.match("^(.*)\r?\n((\r?\n)+)$", tidied_text) if not self.numbers else None

I assume the inclusion of ‘\r’ is for Linux users as it doesn’t register on my Windows system? (Character 13 in old money :wink: )[/quote]

Acutally, ‘\r’ is windows, ST2 might not return it, but it is just habits though (and it hurts nothing :wink:).

Oops! Pushed fix.

I will issue a request for you to pull it.

[quote=“agibsonsw”]
Didn’t realise the follow code would be so simple:

# Open in web browser desktop.open(the_html.name)
Couldn’t this last step be another setting? Not everyone might want to preview. However, the code would have to back-track a little, so that it saves the file for the user. But, again, this could be decided later. Actually, you’re probably right: if their going to save it as HTML then they probably want to preview straight-away, and then they have control over saving it :smile:[/quote]

If desired this could easily be a setting (simple copy of temp file to output location), but this way you don’t have to worry about what to call the file if it doesn’t exist on disk, and the user can just select save as in there browser and choose whatever name they want.

[quote=“agibsonsw”]
If putting the filename at the top of the HTML then it could, perhaps be formatted as a heading. But, as this is just code, I don’t suppose most people would mind/care. However, it might be worth considering following it with a date-stamp(?).[/quote]

Also very easy. Actually the filename could just be a setting, along with time stamp. The important thing is when you have line numbers you need a ‘
’ there so you can copy the first line number (at least it was this way in Chrome). But a simple change could make it configurable or insert a simple new line if it was disabled.

Cool.

0 Likes

#70

Mmm, what? This seems out of context in the containing para. I assume your confirming that, when using li’s, there needs to be at least a
to force “a gap”.

Actually, ‘
’ is XHTML, ‘
’ is, I believe, HTML5 and ‘
’ is … old skool :smile:

m = re.match("^(.*)\r?\n((\r?\n)+)$", tidied_text) if not self.numbers else None

and is this recent (ternary) Python syntax? I didn’t come across it in my, admittedly dated, book.

0 Likes

#71

@facelessuser

Sorry to be a pain. I’ve pulled in your code, but how do I update my local file copy to this as well? I could just copy it but assume there’s a proper way.

Added: and where do I stick this desktop folder?

0 Likes

#72

This can be ignored, on copy and paste, it seemed the first line number could not get selected for copy (visually), but in actuality, the list info gets picked up just fine.

[quote]m = re.match("^(.*)\r?\n((\r?\n)+)$", tidied_text) if not self.numbers else None
and is this recent (ternary) Python syntax? I didn’t come across it in my, admittedly dated, book.[/quote]

Yep. Makes it nice a short for simple statements.

Oh, I issued the pull request.

Depends on the tool you are using. What are you using to work with git repositories? Basically you need to fetch from origin and merge locally. Some tools make it a one step sync thing.

0 Likes

#73

[quote=“agibsonsw”]
Added: and where do I stick this desktop folder?[/quote]

Packages/PrintHtml │ .gitignore │ package-metadata.json │ PrintHtml.py │ PrintHtml.pyc │ PrintHtml.sublime-settings │ readme │ └───desktop dialog.py windows.py __init__.py

It should all be under your plugin folder.

0 Likes

#74

@facelessuser

Copying from a web page, it’s sometimes awkward to catch the very beginning of text - Shift-Left is useful.

They should have introduced a proper ternary operator: it’s Python-like ? anyway.
(In JS I can also ‘whatever = whatever || nufink’ without pointless ‘whatever = Doh’ assignments.)

I’m using GitBash. I keep meaning to read into this subject… looks like I’ll have to now :wink:

Interesting graphic - although could be clearer :smile:. Underneath Packages/PrintHtml, ta.

Regards, Andy.

0 Likes

#75

@facelessuser: Cool, all working now.

There’s a slight pause for the browser, but the code itself runs very quickly. Don’t think it requires a thread.

I considered, briefly, removing the default scope from all dictionary entries: this shouldn’t affect the scope search/match. But it probably takes longer to remove these than it does to perform (very efficient) dictionary lookups.

I’ll have to look into synch-ing my local copy, but other than that I’m at a loose end :smile:.

Q. Is the default font for linux/apple also Consolas?

Should wait for feedback now, although this is not often forthcoming on this forum. Regards, Andy.

0 Likes

#76

Just used tree on windows

tree /F

It made the root folder C:. so I changed it to something that made more sense.

[quote=“agibsonsw”]
Q. Is the default font for linux/apple also Consolas?

Should wait for feedback now, although this is not often forthcoming on this forum. Regards, Andy.[/quote]

No and no.
Not sure what the default is, but you could just use a common monospace font. Consolas is a windows font. I use it on my Mac, but it is not on there by default. I think Courier New is pretty common and monospace. Maybe I am wrong, but it is common.

0 Likes

#77

[quote=“SeanWcom”]Hey guys, just wanted to chime in with an error I was seeing, and what I did to fix. Python isn’t my strong point at all, so there’s likely something else wrong. But hopefully something will help. :smile: I’m using the last pasted code from above, rather than the github repo’s code. When I press ctrl-alt-n, I get an error:

Traceback (most recent call last): File ".\sublime_plugin.py", line 356, in run_ return self.run(edit, **args) File ".\PrintHtml.py", line 18, in run File ".\xml\dom\minidom.py", line 1918, in parse File ".\xml\dom\expatbuilder.py", line 922, in parse IOError: [Errno 22] invalid mode ('rb') or filename: u'D:\\MyStuff\\Dropbox\\Software\\Sublime Text 2\\Data\\PackagesD:\\MyStuff\\Dropbox\\Software\\Sublime Text 2\\Data\\\\Themes - Light\\Colorized-704864249585081-Espresso Soda.tmTheme'
You can see the path to the theme is very crazy. I changed this:

doc = minidom.parse(path_packages + colour_scheme)

To this:

doc = minidom.parse(settings.get('color_scheme'))

And it works fine… I’m running in an irregular setup though. I’m using the USB version of ST2, and you can see in my path that I keep it stored in my Dropbox. So I don’t have the install in its usual location… not sure if that’s part of the problem or not. Oh, and I also created new locations for light and dark themes (as you can see in the path) to help my theme organization.

Hopefully something here helps you out… I’d love for this to be working 100%. :smile: Lastly, would it be possible to add a keybind for copying the html to the clipboard?[/quote]

  1. Yeah, I tweaked my dark theme into a light theme as well. And with the new setting to point at your color scheme, you can now specify.

  2. Yeah, to be honest, we probably should just use the colour_scheme setting directly since the ST2 environment’s default location is right above Packages. That way relative to ST2 or explicit paths would be okay.

0 Likes

#78

@facelessuser

+ self.font_face + '\", Consolas, Monospace;')

On reflection, I think this is okay - Mac, etc., will use their default monospace font anyway. (I also ignored the fact that Consolas might appear twice he, he.)

BTW: (off topic) I still continue, off and on, to wrestle with Python.tmLanguage. I rejected your version (Sorry!) and I managed to get it working pretty well - matching brackets and all. But a chain.of.words.including.my.own.specials is still gobbled up by dotted-name. I know I could add/repeat my terms in the repository, but I cannot detach from the idea that dotted name can be modified (or deleted!) to behave properly. Perhaps by introducing \G somewhere. Anyway, just a point of (limited) interest.

Regards, Andy.

0 Likes

#79

@facelessuser

ah, oopz, dunno what was up with that

i just copy/pasted it

but yeah, plistlib is your friend

0 Likes

#80

I said I would publish this Wednesday - I meant Thursday, the weekend has thrown out my calendar?!

Having split the text into numbered items (li’s) I realise I should also have split the view content into lines as well. This has several advantages:

I wouldn’t need a complex process to find, and remove, the ‘\n’ newline characters;
A simple if, or else, would determine that there was a completely blank line;
The likelihood of an empty span would be much reduced;
A nice generator function could process each span;
The file could be written-to one line at a time.

Also, perhaps, the spans’ text and colours could be kept in a temporary list of tuples, and then HTML-encoded, etc., in one shot (per line), before being written to the file.

I know it works *great *as it is, but I think it might be worth the effort to redraft. I shall consider, and perhaps tackle, this tomorrow. Andy.

0 Likes