Sublime Forum

ExportHtml Plugin (rename from PrintHtml)

#65

[quote=“agibsonsw”]I grabbed the following code from the Python docs and ran it (Windows 7) and it sent an email for me :smile:

The SMTP settings can be added to the package settings and send() might be replaced with display() or show().

But perhaps it’s not so straight-forward on non-Windows OSes(?).

[code]import smtplib

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

me == my email address

you == recipient’s email address

me = "my@email.com"
you = "your@email.com"

Create message container - the correct MIME type is multipart/alternative.

msg = MIMEMultipart(‘alternative’)
msg’Subject’] = “Link”
msg’From’] = me
msg’To’] = you

Create the body of the message (a plain-text and an HTML version).

text = “Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org”
html = “”"\

Hi!
How are you?
Here is the link you wanted.

"""

Record the MIME types of both parts - text/plain and text/html.

part1 = MIMEText(text, ‘plain’)
part2 = MIMEText(html, ‘html’)

Attach parts into message container.

According to RFC 2046, the last part of a multipart message, in this case

the HTML message, is best and preferred.

msg.attach(part1)
msg.attach(part2)

Send the message via local SMTP server.

s = smtplib.SMTP(‘localhost:25’)

sendmail function takes 3 arguments: sender’s address, recipient’s address

and message to send - here it is sent as one string.

s.sendmail(me, you, msg.as_string())
s.quit()[/code][/quote]

Yeah, I use smtplib at work to email myself stuff when certain events occur. If you use something like minirelay or hMailServer on windows, you can set up your own outgoing smtp service on that computer bypassing having to use an external smtp server.

On Mac, and I believe Linux, you can use mailx to send out mail. On mac it uses username@yourpc…something like that. I think in Linux you can actually specify the sender, so yeah sending mail is completely possible.

0 Likes

#66

Simple example that works on Mac or windows (assuming you have a smtp relay setup on windows so you can use localhost as your outgoing smtp server; notice no authentication is needed unless you configure it that way). Keep in mind this is not very robust, only an example.

mailx was a little broken out of the box on Mountain Lion. To fix on Mountain Lion

sudo mkdir -p /Library/Server/Mail/Data/spool sudo /usr/sbin/postfix set-permissions

[pre=#000000]import platform

def send_mail_windows(receiver,* subject*,* body*,* sender*,* server*):
import smtplib
from email.mime.text import MIMEText
msg = MIMEText(body)
msg’to’] = receiver
msg’from’] = sender
msg’subject’] = subject
smtpObj = smtplib.SMTP(server)
smtpObj.sendmail(sender, receiver], msg.as_string())
smtpObj.quit()

def send_mail_mac(receiver,* subject*,* body*):
import subprocess
cmd = “”“echo “%s” | mailx -s “%s” %s”"" % (body, subject, receiver)
p = subprocess.Popen(cmd, shell=True,* stdout*=subprocess.PIPE)
output, errors = p.communicate()
if errors != None:
print errors
if output:
print output

system = platform.system()
if system == “Darwin”:
send_mail_mac(
‘me@gmail.com’,
‘Mail Time’,
“Look mom, I am sending an email!”
)
elif system == “Windows”:
send_mail_windows(
‘me@gmail.com’,
‘Mail Time’,
“Look mom, I am sending an email!”,
‘noreply@CaptainLaserFace.com’,
‘localhost’
)
else:
# Do some linux stuff, probably use mailx
pass[/pre]

0 Likes

#67

As I have found here at my office, our proxy blocks outbound SMTP calls directly. I’d found a way to make calls to send mail through Exchange via a VB script, but that probably won’t help much here.

My point about sending via email was just an example. There are plenty of times where I want to paste code snippets into Word documents SharePoint (yuck) pages, or even (more frequently for me) Evernote where I can save off commonly used snippets of code for searching later.

If we want to email our happily formatted code, is it possible to make a new plugin that piggy-backs off of this one?

0 Likes

#68

[quote=“Grant”]As I have found here at my office, our proxy blocks outbound SMTP calls directly. I’d found a way to make calls to send mail through Exchange via a VB script, but that probably won’t help much here.

My point about sending via email was just an example. There are plenty of times where I want to paste code snippets into Word documents SharePoint (yuck) pages, or even (more frequently for me) Evernote where I can save off commonly used snippets of code for searching later.

If we want to email our happily formatted code, is it possible to make a new plugin that piggy-backs off of this one?[/quote]

Don’t worry, I have no intention of supporting email in this plugin. The code I posted was an example because agibsonw brought it up. I tried to get a script to send emails directly with outlook at work, but it is a pain. I was just showing a basic example I use at work to send emails to myself with some fake email. It works great to notify myself of events when I am away from my computer, but I do not want to write a plugin where I have to support X mail server and Y mail protocol because firewall Z blocks A etc. I just don’t want to support stuff like that.

I will be happy if I can get the HTML formatted properly for clipboard with the major the major OS variants…assuming I can get that done :smile:.

0 Likes

#69

@facelessuser

Soz. I was just pleasantly surprised that the email code worked out of the box :sunglasses:. But, on reflection, I realise that there is a lot more to this than meets the eye. Besides which, the html would need to be stripped of all JS, stylesheets, HTML headers etc… *

Andy.*

0 Likes

#70

Got some new stuff in.

-Theme json is now embedded in the HTML and can be reassembled into a PLIST XML via a function call and extracted. You can enable a setting to display a button to download the theme (chrome will actually download the theme; others will open a window with the plain text xml in it; a limitation in other browsers right now or in my knowledge).
-I have added code to allow for toggling plain text mode of the exported html (it can only be accessed right now when it is embedded in another webpage, but I plan to expose this via a toolbar that will appear…I am not there yet).
-I also added a feature a couple of weeks ago to save the XMLs in a folder of your choice or in the same folder as the original source. It also has an optional timestamp that is appended to the file that you can disable.

I have a demo page you can play with that shows off the new features: facelessuser.github.com/ExportHtml/

I do have some more plans, but that is it for now.

0 Likes

#71

I have now added a toolbar with access to print, hide/show gutter, toggle plain text, download theme, and toggle showing annotation table (if exists). You can configure these with the option toolbar when running the command. It just takes an array of the string keywords to tell ExportHTML which (if any) toolbar items you want to show.

I will be removing the text in favor of icons as soon as I get the time to make the icons. After that, I will stop procrastinating the finishing of BracketHighlighter2.

Demo page has been updated with recent changes facelessuser.github.com/ExportHtml/.

0 Likes

#72

@facelessuser

You’re a nutcase (In a good way :smile:)

0 Likes

#73

[quote=“castles_made_of_sand”]@facelessuser

You’re a nutcase (In a good way :smile:)[/quote]

:smile:

On a side note, the toolbar is complete. Demo page is updated.

0 Likes

#74

All changes are in. I fixed some stuff and added the ability to toggle wrapping via the toolbar (if wrapping was enabled). I am done fooling with this for now. Hope you guys like the changes. As always, feel free to report bugs or give feedback. Now I will get back to work on BracketHighlighter2.

Version 0.5.0

  • Added ability to define path to save generated html to a specific folder with optional timestamp
  • If selection foreground is not defined, use normal colors for text.
  • Click annotations to show annotation jump table
  • Removed shortcut actions
  • Themes are now embedded in the html and can be extracted
  • Added toggle plain text option and toggle wrapping (if enababled)
  • Added toolbar to print, download theme, disable toggle wrapping (if enabled), toggle annotation jump table (if annotations available), toggle plain text, and toggle gutter
  • Exposed toolbar options in configuration (can define any toolbar item to appear)
  • Split out javascript into separate files
  • Improved and fixed javascript issues
0 Likes

#75

@facelessuser - just curious what JS issues? I assume nothing too significant…

0 Likes

#76

No nothing noticeable. Mainly sloppy coding on my part. And tried to be more efficient about certain things.

0 Likes

#77

Really wasn’t planning on touching this again so soon, but there where a number of IE bugs (no surprise there), and I really wanted to add support for scopes that use background colors. That’s it for real this time :smile:.

-Fix plain text mode in IE8
-Fix plain text mode copy paste IE(remove syntax highlight so IE copy and paste doesn’t copy hidden elements)
-Fix stippled boxes around toolbar icons when clicked in IE
-Fix sorting of object keys in a way compatible with IE<9
-Fix issues where lines would show gaps between lines in multi-line highlights
-Fix issues in IE when gutter is disabled empty rows collapse
-Move CSS to separate file
-Add global option to configure toolbar orientation (horizontal|vertical)
-Add support for background colors of scopes in theme file

0 Likes

#78

Export Html currently only selects one theme entry to highlight code with, but syntax highlighting themes that heavily use background colors actually have some more dynamic interaction allowing essentially multiple definitions to highlight one bit of text. So one theme entry may paint the background, but another entry will paint the foreground. Themes like this, ExportHtml can render quite ugly. Just an FYI. I might add in a temporary switch to disable background color painting until I get better support in, we shall see. My real goal is to get better background support though. Maybe in a couple of weeks.

0 Likes

#79

I got a little ambitious, and added some better rules for selecting background colors etc. and also actually process transparent colors by simulating what they should look like. I would post an example, but its late.

-Simulate transparency
-More dynamic rules for colors and styles

0 Likes

#80

This is lame dude, I can’t even change theme on the fly :wink:

0 Likes

#81

lol, I know right :smile:.

So here is an example of the new background handeling.

This is ST2

Here is Export HTML (notice I don’t pad the end of the lines yet with color, but generally it looks pretty good…except for the horrible theme :smile:).


Transparency might not be exactly spot on, but it is pretty close. I have to guess how ST2 might be doing it, and then I have a little fudge factor in their to adjust things. In the example above, I believe the reddish and purplish colors use transparency, so they are pretty close.

The padding is trickier than you would think. I am trying to keep the text in tables so I can easily copy and paste any section to an email (which I use a lot). And I am also trying to preserve wrapping for printing. All of these things can be at odds with each other…and exhausting. This is also why, if you use certain fonts, you may see a break between code lines. It is hard for me to get the spans to fill in those lines because they don’t respond to height properites, but I can’t check their pixel height at compile time in python, I would have to do that at run time and then pad every span…ugh. If I remove the table, the problem goes away, but then if I copy and paste a section of the HTML, I lose page background color, and it loses the uniform right edge. If I use block elements and float them left, I lose the easy way to test for line length and wrap them. Web coding can be a real pain.

0 Likes

#82

[quote=“facelessuser”]castles_made_of_sand wrote:
This is lame dude, I can’t even change theme on the fly

lol, I know right .[/quote]

Glad you got the joke :smile:

0 Likes

#83

Okay. Background support is done. I think I may have also solved the gaps between rows with certain fonts on certain platforms.

This is generated by ExportHTML, not ST2 :smile:.


That’s all.

0 Likes

#84

One more thing. The new background coloring feature caused one side affect for people me when copying and pasting code into Outlook.

Apparently, if outlook has html code pasted in it with spans who have a color darker than about 62 luminescence, it renders all the text in those spans as white. Kind of annoying. So I added a feature that the few people who paste dark themes into emails in Outlook might use. The feature causes ExportHTML to scale up the entire theme to ensure that all background colors are at least 62 luminescence (this can be configured to a different value if desired; between 0-255). I also fixed an issue where some themes wouldn’t work because I hadn’t encoded the plist proper that I stuff into the HTML; that is now fixed as well.

Example:
[pre=#2D2D2D] // Minum allowed background lumens a.k.a brightness (0-255)
“bg_min_lumen_threshold”: 62,

    // Browser view color (selections and multi-selections allowed)
    {
        "Browser View - Current Color (EMAIL)": {
            "numbers": true,
            "multi_select": true,
            "shift_brightness": true
        }
    },[/pre]
0 Likes