Sublime Forum

Copy text with highlighted colors

#95

Wow, you’re up-to 300 lines! Not much point in looking at my previous code sample then :laughing:

[quote]shufty
The English form of shufti (arabic for look / take a look)

Originally RAF but later Army also, shufty or shufti became taking a look for possible dangers. [/quote]

0 Likes

#96

I am actively using it at work, so some of these issues have cropped up for me, so I fix them inline sometimes as I am going. All the major work happened last night…and I regret how much time I spent.

The lines collapsing is why I reverted to using the pre. I no longer had to capture the \n. For some reason I think I still strip them and I just right the single one at the end. I think I was sometimes getting phantom ‘\n’. I need to look at that, I might not need to do that anymore.

0 Likes

#97

[quote=“agibsonsw”]Wow, you’re up-to 300 lines! Not much point in looking at my previous code sample then :laughing:

[quote]shufty
The English form of shufti (arabic for look / take a look)

Originally RAF but later Army also, shufty or shufti became taking a look for possible dangers. [/quote]

[/quote]

Yeah. Like I said…too much time. I am probably taking a break the next couple of days.

0 Likes

#98

I noticed you were still replacing ‘\n’ with ‘’ when split_by_newlines should remove this need - but it can’t do any harm.

I’ll read through your code in more detail tomorrow. Laterz, Andy.

0 Likes

#99

@facelessuser

On reflection, I won’t be releasing this myself as a plug-in. I’m not a programmer by trade, just an enthusiast, and this is not a feature that I will use often myself. Therefore, I’m unlikely to retain enough interest in it to maintain, and support, a plug-in.

You have (now!) invested more time in it than I and, as you stated, it’s something that you are likely to continue to use. So I’m happy for you to release it yourself, and shall continue to take an interest in your code :sunglasses:, and support your progress.

If it doesn’t interfere, I’ll not pull your code to mine, as there are still a few things I want to do with my (little…) version.

Regards, Andy.

Added: How do I cancel your pull request, or do you need to withdraw it? As I’m unable to update/commit my own update at the moment.

0 Likes

#100

[quote=“agibsonsw”]@facelessuser

On reflection, I won’t be releasing this myself as a plug-in. I’m not a programmer by trade, just an enthusiast, and this is not a feature that I will use often myself. Therefore, I’m unlikely to retain enough interest in it to maintain, and support, a plug-in.

You have (now!) invested more time in it than I and, as you stated, it’s something that you are likely to continue to use. So I’m happy for you to release it yourself, and shall continue to take an interest in your code :sunglasses:, and support your progress.

If it doesn’t interfere, I’ll not pull your code to mine, as there are still a few things I want to do with my (little…) version.

Regards, Andy.

Added: How do I cancel your pull request, or do you need to withdraw it? As I’m unable to update/commit my own update at the moment.[/quote]

Not a problem. I have to say, for only an enthusiast, you seem to have the brain of a programmer :smile:. This was an excellent idea, and you broke it down well.
Of course, you will have the credits for this when/if it gets released.

Now there seems to be a couple of plugins that do the same thing…well not exactly the same (yours is the only one that seems to duplicate sublimes colors). I will have to evaluate if people want another plugin for converting to HTML. But nevertheless, I am going to run with mine; I find it fun tinkering with this :smile:.

I will let you know if it gets released.

A couple of side notes that I plan to be working on.
-A setting to enable a prettier HTML wrapping. I don’t plan on adding indentation, but just forcing the wrap after the line numbers at a certain user specified size. (probably use JavasScript)
-Maybe a menu for printing different configurations.
-Add a simple gray scale print color scheme.

0 Likes

#101

@facelessuser
A grey-scale option occurred to me a while ago :wink:

Wouldn’t JS be overkill? I removed the left-margin (when not printing line numbers) with a couple of css-tweaks.

I’ve posted the code below in case it has anything useful to you - but I’m still using a list.

I’m building a temporary string of the spans before writing them;
I’m able to skip a line entirely if it’s empty;
I’m stripping ‘\r\n’ in case there are any, particularly ‘\r’, left over;
I’m only replacing spaces with nbsp if they occur at the beginning of the span. This is helpful when copying the HTML, but I don’t know how it would affect your* fake alignment* of, for example, dictionary assignments;
I’m not writing the temporary line if it’s empty.

[code]
def convert_view_to_html(self, the_html):
for line in self.view.split_by_newlines(sublime.Region(self.pt, self.size)):
self.pt = line.begin(); self.end = self.pt + 1
if line.empty():
the_html.write(’
\n

  • ’)
    continue
    self.line_end = line.end()
    temp_line = ‘’
    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
    region = sublime.Region(self.pt, self.end)
                the_colour = self.guess_colour(scope_name.strip())
                tidied_text = self.view.substr(region)
                tidied_text = tidied_text.replace('&', '&amp;')
                tidied_text = tidied_text.replace('<', '&lt;')
                tidied_text = tidied_text.replace('>', '&gt;')
                tidied_text = tidied_text.replace('\t', '&nbsp;' * self.tab_size)
                tidied_text = tidied_text.strip('\r\n')
                if len(tidied_text):
                    without_init_sp = tidied_text.lstrip(' ')
                    init_spaces = len(tidied_text) - len(without_init_sp)
                    if init_spaces:
                        without_init_sp = ('&nbsp;' * init_spaces) + without_init_sp
                        tidied_text = without_init_sp
                    temp_line += '<span style=\"color:' + the_colour + '\">'
                    temp_line += tidied_text + '</span>'
                self.pt = self.end
                self.end = self.pt + 1
            if temp_line != '':
                the_html.write(temp_line)
            the_html.write('</li>\n<li>')[/code]
    

    [Copied from HTML :smile: ]

    There might be a neater way of replacing the spaces only at the beginning of the line :question: (non-regex).

    I tried ‘closing’ your pull request but I’m still unable to update to my local version :question:

    Added: Oops. My version doesn’t copy the line numbers, Doh! I’ve only just noticed this :laughing:. I think you may have hinted at this earlier :wink:
    Added again: No worries, I can always copy/save the HTML if I want the line numbers.

  • 0 Likes

    #102

    I thought it might seem like overkill at first, but it seems to work quite well.

    I just write this to the end of the document feeding in the right parameters. User can define wrap size (px).

    [code]WRAP =
    “”"

    “”"[/code]

    With this method, if you copy the text, the wrapped lines will still be part of the same lines, not separate. I am still goofing around, this could change. It is easier this way, because then I can just grab the size of the number column and resize the content of text column on the fly. I have to guess size if I try to do it at HTML creation time. There are a lot of factors: font, font-size, largest row number, etc.

    0 Likes

    #103

    Meant style.width not offsetwidth on the file info one. It is fixed.

    0 Likes

    #104

    @facelessuser

    I closed your pull request but was still having trouble updating from my local copy. It asked me to pull first. Anyway, I managed to commit my update but the file includes some weird annotations:

    [code]<<<<<<< HEAD

    e1ec88f044e2ef6bc61552215c2e77e3f61b9ec2

    class PrintHtmlCommand(sublime_plugin.TextCommand):[/code]
    Have you seen this before? Andy.

    **Added **No worries, I committed again and it’s fine. Don’t know why I had to pull first though…

    0 Likes

    #105

    Considering myself a JS guy :wink: it’s just

    [code]
    // or, if you must
    //

    [/code] I haven't read your code in detail but I'm sure you're aware that attempting to read **width** if it hasn't been set can lead to unexpected return values (in different browsers). But a quick glance tells me that you've set it to a value anyway :smile:
    0 Likes

    #106

    [quote=“agibsonsw”]Considering myself a JS guy :wink: it’s just

    [code]
    // or, if you must
    //

    [/code] I haven't read your code in detail but I'm sure you're aware that attempting to read **width** if it hasn't been set can lead to unexpected return values (in different browsers). But a quick glance tells me that you've set it to a value anyway :smile:[/quote] Yeah, I was a little sloppy (mainly just tinkering right now). I haven't done javascript since I wrote a plugin for the TonidoPlug (I have been taking a long javascript break since then). I find JS fun, but I find all the different web browser inconsistencies not fun. Some of it isn't as fresh in my brain. I will be running it through the browser gamut when I get closer to a finalized plugin. I probably need to reference my old JS code to remember everything; I really got deep into JS and browsers when I was doing it.
    0 Likes

    #107

    I’m keen on JS (wrote my own library :smiley: ) which makes me dislike all the fake Python assignments ( something = why! ). So let me know if you need a second opinion at any stage.

    Andy.

    0 Likes

    #108

    [quote=“agibsonsw”]I’m keen on JS (wrote my own library :smiley: ) which makes me dislike all the fake Python assignments ( something = why! ). So let me know if you need a second opinion at any stage.

    Andy.[/quote]

    Thanks, I will let you know if I have any specific questions. That TonidoPlug plugin I mentioned, it has my javascript library built into it too :smile: (I got really in to JS for that span of time). But as with anything use it or lose it. The good thing about this plugin, it should be very light on JS. I think I really on have two small applications for it right now: open browser print dialog, and wrap html at XXX px. I want to keep it very very light.

    0 Likes

    #109

    @facelessuser: Won’t be as light as mine - I’ve finished for the moment :wink:

    [quote] def write_header(self, the_html):
    header = CSS % (
    path.basename(the_html.name), # Title
    str(self.font_size), # Code font size
    self.font_face, # Code font face
    self.bground, # Page background color
    self.gbground, # Gutter background color
    self.fground # Default text color
    )
    the_html.write(header)[/quote]

    Named parameters are cool:

    print '<a href="%(url)s">%(url)s</a>' % {'url': my_url}

    but it’s probably not worth amending. It just means you/we wouldn’t have to worry about the order, or comments.

    0 Likes

    #110

    I will have to check it out :smile:. Since my requirements are slightly different, I probably cannot pull in your format, but if it works better and fulfills my requirements…I do like lighter.

    [quote=“agibsonsw”]

    [quote] def write_header(self, the_html):
    header = CSS % (
    path.basename(the_html.name), # Title
    str(self.font_size), # Code font size
    self.font_face, # Code font face
    self.bground, # Page background color
    self.gbground, # Gutter background color
    self.fground # Default text color
    )
    the_html.write(header)[/quote]

    Named parameters are cool:

    print '<a href="%(url)s">%(url)s</a>' % {'url': my_url}

    but it’s probably not worth amending. It just means you/we wouldn’t have to worry about the order, or comments.[/quote]

    That would be pretty useful. Man , why didn’t I realize I could do that. I will pick that up for sure.

    0 Likes

    #111

    @facelessuser

    You can also repeat an argument

    (some_fn(), ) * x

    (I’m sure you know this…) but I don’t think it’s relevant.

    **

    0 Likes

    #112

    Well here’s my updated repository on GitHub.

    @facelessuser: They only minor changes worth mentioning are that:

    I’ve set ‘curr_row = 1’ for numbers/not partial, just to leave the future possibility that I might want to number them differently.

    And, borrowing from syntax you introduced me to:

    # Get general theme colors from color scheme file self.bground = colour_settings"background"] if "background" \ in colour_settings else '#FFFFFF' self.fground = colour_settings"foreground"] if "foreground" \ in colour_settings else '#000000' self.gfground = colour_settings"gutterForeground"] if "gutterForeground" \ in colour_settings else self.fground
    This also means I don’t have to check ‘if self.fground == ‘’’ when creating the style-rules (or initialize them to ‘’ ).

    0 Likes

    #113

    Just trivia:

    I spent ages racking my brain to obtain a neat way of replacing the spaces, but only at the beginning of a string. After a few attempts I ended up with:

    if len(tidied_text): init_spaces = len(tidied_text) - len(tidied_text.lstrip(' ')) if init_spaces: tidied_text = (init_spaces * '&nbsp;') + tidied_text.lstrip(' ')
    But it made me smile as this was practically the very first (and best) method that I had tried :laughing: :sunglasses:

    0 Likes

    #114

    [quote=“agibsonsw”]Well here’s my updated repository on GitHub.

    @facelessuser: They only minor changes worth mentioning are that:

    I’ve set ‘curr_row = 1’ for numbers/not partial, just to leave the future possibility that I might want to number them differently.

    And, borrowing from syntax you introduced me to:

    # Get general theme colors from color scheme file self.bground = colour_settings"background"] if "background" \ in colour_settings else '#FFFFFF' self.fground = colour_settings"foreground"] if "foreground" \ in colour_settings else '#000000' self.gfground = colour_settings"gutterForeground"] if "gutterForeground" \ in colour_settings else self.fground
    This also means I don’t have to check ‘if self.fground == ‘’’ when creating the style-rules (or initialize them to ‘’ ).[/quote]

    Cool, I will check it out.

    Here is a preview of multi-select support :wink:


    0 Likes