Sublime Forum

Copy text with highlighted colors

#117

[quote=“agibsonsw”]@facelessuser: Oh, I see :sunglasses: Very clever, and useful!

I misunderstood; I was thinking, previously, of highlighting the text that they have selected (with their selection colour) - but are you doing this as well?
[/quote]

I meant both:


[quote=“agibsonsw”]
As you’ve mentioned JS :smile: a silly, but interesting idea is to allow text to be highlighted and annotations added using the input panel. These could then become tooltips, or popup boxes in the HTML - even just with CSS. This would make the page dynamic… and cool. But I don’t want you to have another sleepless night.

BTW I might look at Word at the weekend. Not because it’ll be useful, but purely for my Python education. Alternatively, an Outlook/ send to option - which begins an email with the HTML in the body - should not be too tricky. In fact, it may only require a handful of code-lines.

Andy.[/quote]

That would be interesting. Especially if you were looking for an annotated document of your code. I am wondering how useful it would be? It is kind of a cool idea though. Don’t worry about me not sleeping. I am almost done with all features on my immediate todo list. Anything like this would come after I take a nice long break :smile:. I just want to round out the basic features and put it through some good testing and give it a 1.0.0 release version.

0 Likes

#118

[quote=“agibsonsw”]Actually, doesn’t have to be Outlook.

I’ll pursue this a little bit. There’s a mailer module which is a simple wrapper around the ‘email’ module. It’s under an MIT licence :question: but if it’s possible I could copy the source-code and remove the bits I don’t need (as it’s for a quite specific purpose).

If I can’t just *rip *the code then I’ll have to study the email module and build a wrapper myself (I suppose).

Otherwise, should be a breeze. Andy.

Actually, I need to tear off the header, etc., from the_html - leaving just a list or table.[/quote]

MIT is pretty forgiving. You can use it and even modify it as I understand, but you must not claim it as your own and you must provide the license as well or at least have it in the code file. GNU GPL is a bit more annoying (though there are a couple of versions) but in general it kind of infects your project, and the project including it must also be released under the same license. Technically, I think the desktop module that we are including is GNU GPL (that is the one downside of it). But keep in mind, I am no expert in all of this license stuff. For anything we are doing, I don’t think it matters, but combining MIT and GPL, I am not sure how that works together or if it can.

0 Likes

#119

It appears the Desktop module is the Lesser GPL which I think is compatible with MIT. So I think you would be fine.

0 Likes

#120

@facelessuser: Thought you were doing both, as I noticed you storing the ‘selection’ colour. Been a bit pre-occupied to study your code in more detail though :smile:

The mailer source code includes the following comment:

[quote]A lot of this code was taken from the online examples in the
email module documentation:
docs.python.org/library/email-examples.html

Released under MIT license.[/quote]

which seems very noble. At the end of the day, we are ALL *borrowing *code from somewhere :smile:

Actually… just had an inspiring idea about the tooltips/ popup boxes. They could be (editable - TextArea) to-do items/ comments. What a cool idea :sunglasses: :bulb:. But I’m getting a little carried away now…

Andy.

0 Likes

#121

@facelessuser: Signing off now, you’ll be pleased to hear.

I’m v. happy with my code but I think I’ll explore a couple of ideas tomorrow evening - just for my own Python education:

  1. I’m storing the spans in a temporary string before writing to the file. If, instead, I store a list of tuples (tidied_text, the_colour) then I can perform the entity-escaping, etc., in one fell swoop, perhaps with a list comprehension.

  2. I’d like to gain a better understanding of generators. It’s screaming at me that processing spans within a line could be a good example for this. (This is probably an alternative to 1.)

BTW A while ago (yesterday? can’t remember) I did notice ‘\r’ popping up on occasion, but presumably split_by_newlines has removed this issue. Personally, I’m using:

tidied_text = tidied_text.replace('\t', ' ' * self.tab_size).strip('\r\n')

But, as I say, it’s probably no longer an issue.

Laterz, Andy.

0 Likes

#122

[quote=“agibsonsw”]@facelessuser: Signing off now, you’ll be pleased to hear.

I’m v. happy with my code but I think I’ll explore a couple of ideas tomorrow evening - just for my own Python education:

  1. I’m storing the spans in a temporary string before writing to the file. If, instead, I store a list of tuples (tidied_text, the_colour) then I can perform the entity-escaping, etc., in one fell swoop, perhaps with a list comprehension.

  2. I’d like to gain a better understanding of generators. It’s screaming at me that processing spans within a line could be a good example for this. (This is probably an alternative to 1.)

BTW A while ago (yesterday? can’t remember) I did notice ‘\r’ popping up on occasion, but presumably split_by_newlines has removed this issue. Personally, I’m using:

tidied_text = tidied_text.replace('\t', ' ' * self.tab_size).strip('\r\n')

But, as I say, it’s probably no longer an issue.

Laterz, Andy.[/quote]

I think the \r is a non-issue, but as stated earlier, it hurts nothing to try to strip them.

I went ahead and posted the last currently planned features.
-color and black and white default color schemes.
-quick panel access to configurations stored in settings file.
-multi-select available
-option to turn off gutter styling (mainly for when you want to print)
-code cleanup

At this point I just need to do some good testing and write up the documentation.

@agibsonsw
I will keep an eye on what you are working on. Good luck!

0 Likes

#123

working on unicode issues in html.

0 Likes

#124

Unicode fix is now in.

0 Likes

#125

I’m interested to hear a little more detail?

Because we are using an HTML5 DOCTYPE, we could add

<meta charset="UTF-8">

*immediately *after the opening tag, but it isn’t strictly required link here. But I assume you’ve come across more specific issues?

0 Likes

#126

Unicode could not be written to the file for one. That just required some encoding to pass it to the file write command. But then the unicode characters didn’t show up right.

So really what needed to happen was the text needed to be converted to ASCII, but also escaped for HTML to something like &#XXXX;.

So now I just pass the text to a function:


0 Likes

#127

@facelessuser: I came across encode() the other day and was considering ‘xmlcharrefreplace’, although I wasn’t sure how it fitted in to the project. The Python docs don’t indicate that encode() takes varargs - must be out of date.

BTW, and out of interest, I conquered adding comments last night (in my head :wink: ). Here’s my outline, but I’m not sure if it’s worth doing. Nevertheless, it’s interesting :sunglasses::

  1. Create a shortcut to store the current view, point, word under the point - and a comment read from the input panel - in a dictionary of tuples, using the view as key. If there isn’t a proper a-z word under the point, then bail, and it cannot be a selection (just a single point).

Allowing a selection is too messy, as it would cover a number of spans. Similarly, extending the comment across the current span could be messy, particularly if within a comment line. (Why would someone put a comment in a comment, Doh!)

  1. Add CSS rule(s) to a tag such as ‘b’ or ‘tt’, or possibly a little bit of JS, based on a class that would create the tooltip on hover. (JS might use ids instead: e.g. ‘comment’ + pt)
    2b. Format the tag so it stands out. Can’t really use a colour, so perhaps underline or italic. (But perhaps allow yet another setting for ‘comment_colour’.)
    2c. Might need another styled-span, that JS would use to display the tooltip.
  2. During parsing of the lines/spans - but after entity escaping the HTML - examine the comment-points to see if any fall within the current region/span.
  3. If so, compare the word-under-point. If it’s not the same, then bail - they’ve added more code and the comment is no longer relevant.
  4. Find the same word in tidied_text, and replace it with '<b class=“comment” title=“The comment text”>word".
  5. If using JS for the tooltip, would need to insert the (new) styled-span just inside this b-tag, containing the comment text. (It could be positioned to display underneath the line.)

I recall/believe that tooltips can be created with just CSS, particularly CSS3. They could even just be links, but a little JS might be better.

Anyway, I haven’t decided how far I might pursue this. Although, it’s still kinda cool :sunglasses:

I shall have a look at your current work in a while. Regards, Andy.

0 Likes

#128

@facelessuser

He, he, I was looking at this yesterday, and wondered why ST didn’t offer the second, default, argument. But it does - must have been in my blind-spot when I glanced at the docs.

Curious as to why you created theme files, rather than just creating a dictionary? Perhaps you feel users would be more comfortable modifying the plist.

My **idiom **(notice the Python reference there?) would be to delete all the ’ self.bground = ‘’ ’ as they are now *always *assigned a value, but we had a similar discussion before :wink:

Are ‘activeGuide’, etc., standard pList items in ST? Or have you made them up for the plug-in? (I’ve struggled to find a list of the possible items.)

Andy.

0 Likes

#129

[quote=“agibsonsw”]@facelessuser

He, he, I was looking at this yesterday, and wondered why ST didn’t offer the second, default, argument. But it does - must have been in my blind-spot when I glanced at the docs.

Curious as to why you created theme files, rather than just creating a dictionary? Perhaps you feel users would be more comfortable modifying the plist.

My **idiom **(notice the Python reference there?) would be to delete all the ’ self.bground = ‘’ ’ as they are now *always *assigned a value, but we had a similar discussion before :wink:

Are ‘activeGuide’, etc., standard pList items in ST? Or have you made them up for the plug-in? (I’ve struggled to find a list of the possible items.)

Andy.[/quote]

I just took the Monokai Bright Color Scheme file and modified it. I figured it needed to be in accordance with other scheme files. People can modify them if they want. I left everything in the settings section so it would be functional if some used it (I can’t imagine people using the grayscale one though). In the gray scale one, I gutted everything scope wise because everything is black accept comments which is gray. If something was italic or something, I left it in.

‘activeGuide’ controls the color of the stippled tab guides.

People can use my other plugin and convert the plist to a JSON file if they like that better for modifications. But I wanted to be universal with how I handle all input colors schemes.

0 Likes

#130

@facelessuser: I like this :sunglasses:

[code]class PrintHtmlCommand(sublime_plugin.WindowCommand):
def run(self, **kwargs):
view = self.window.active_view()
if view != None:
PrintHtml(view).run(**kwargs)

class PrintHtml(object):
def init(self, view):
self.view = view[/code]
I still prefer my idea of skipping past tabs and spaces, to reduce the number of spans:

while self.end <= self.line_end: scope_name = self.view.scope_name(self.pt) while (self.end <= self.line_end and (self.view.scope_name(self.end) == scope_name or (self.view.substr(self.end) in '\t', ' ', '']))): self.end += 1
but, then again, you need to keep an eye out for highlights and multi-selects.

The penny hasn’t quite dropped: are you using a table of two-columns, one for the gutter, and then spans in the second column? And using nbsp to keep your alignment?

0 Likes

#131

Pretty much. Just two table columns. Text content is always in a styled span.

For code content I wrap it in a float:left div with width set to 100% and word break enabled. When I need to wrap, I just diff the column sizes and and then set the code column size to finite width.

CSS

<style type="text/css"> pre { border: 0; margin: 0; padding: 0; } table { border: 0; margin: 0; padding: 0; } div { float:left; width:100%; word-wrap: break-word; } .code_text { font: 12pt "Consolas", Consolas, Monospace; } .code_page { background-color: #FFFFFF; } .code_gutter { background-color: #E5E5E5;} span { border: 0; margin: 0; padding: 0; } body { color: #000000; } </style>

html line output

[code]

   1 
 {
[/code]
0 Likes

#132

@facelessuser

Sorry, being a bit dim… You mention ‘text content’ and ‘code content’, but I can’t see the distinction in your code (yet). Do you mean that the 2nd column (containing the code) always contains a div?

And, as per your html output sample, are you using nbsp to align the line-numbers?

I hope the width behaves for you in other browsers :wink:

0 Likes

#133

@facelessuser: I suspect it should be:

encode('utf-8', 'xmlcharrefreplace')

If you select some text to print, with the end point in the middle of a word, does it sometimes include an extra (following) character? Might just be mine :smile: and it’s not a biggie. Added: scrub this question - sorted. Andy.

0 Likes

#134

[quote=“agibsonsw”]@facelessuser: I suspect it should be:

encode('utf-8', 'xmlcharrefreplace')

[/quote]

No. ‘ascii’ is correct. I have already tested it.

[quote=“agibsonsw”]
If you select some text to print, with the end point in the middle of a word, does it sometimes include an extra (following) character? Might just be mine :smile: and it’s not a biggie.[/quote]

Select in sublime? It used to, then I fixed it :wink:.

0 Likes

#135

Oh, on a side note. I think I am going to add a shift click to hide/show the line numbers.

0 Likes

#136

Do you mean in the HTML output, using JS? Or just a shortcut in ST to hide the gutter (I kinda assumed there would already be one somewhere to do this?).

I wrote a JS bookmarklet previously, so that I could hide elements on a page by clicking :wink:

javascript:(function(){var d=document,useMine=true,prevEl;function AddHandler(orig,mine) {return function(e){if(useMine)mine(e);else if(orig)orig(e);};}function Myonmouseover(e) {var evt=e||window.event;var elem=evt.target||evt.srcElement;elem.style.outline='2px solid gray'; prevEl=elem;}function Myonmouseout(e){var evt=e||window.event;var elem=evt.target||evt.srcElement;elem.style.outline='';} function Myonclick(e){var evt=e||window.event;var elem=evt.target||evt.srcElement;elem.style.display='none';} function Myonkeydown(e){var evt=e||window.event;if(evt.keyCode==27){prevEl.style.outline='';useMine=false;}} d.onmouseover=AddHandler(d.onmouseover,Myonmouseover);d.onmouseout=AddHandler(d.onmouseout,Myonmouseout); d.onclick=AddHandler(d.onclick,Myonclick);d.onkeydown=AddHandler(d.onkeydown,Myonkeydown);})()

But if I click a line number the whole page disappears :laughing: because I’m actually clicking the ‘ol’ !

Andy.

0 Likes