Skip to content
Cade Gillem edited this page Apr 14, 2015 · 2 revisions

Publisher is a module written by cpg to provide functionality for converting markdown to HTML/CSS and PDF formats.

Tutorial

Sending a formatted email

Code snipped also found at cadegillem.me.

import publisher
import smtplib

message = publisher.md_to_html_email("# Title\nThis is some **markdown!**\n", "h1 { color: blue}")
message["Subject"] = "Markdown Test!"
message["From"] = "[email protected]"
message["To"] = "[email protected], [email protected]"

smtpobject = smtplib.SMTP('localhost')

try:
    smtpobject.sendmail(message["From"], ["[email protected]", "[email protected]"], message.as_string())
except:
    print("Could not send mail.")

Creating an HTML document

import publisher

publisher.to_file(publisher.md_to_html_document(publisher.from_file("test.md"), publisher.from_file("test.css")))

API Reference

  • function from_file(filename): Reads the text content of a file. This can be markdown, HTML, CSS, or anything else.
    • filename: filename to read from with path
  • function to_file(source, filename): Writes text to a file, overwriting any data.
    • source: Text to write to the file.
  • function md_to_html(source_md): Takes markdown syntax with and turns it into HTML.
    • source_md: Markdown syntax to convert
  • function css_to_html_tag(source_css): Takes CSS and turns it into an HTML style tag.
    • source_css: CSS styling
  • function html_to_pdf_file(source_html, output_pdf_filename, source_css_filename): Uses pdfkit to create a PDF file out of HTML with optional CSS.
    • source_html: The HTML source of the file
    • output_pdf_filename: Filename of the destination PDF
    • source_css_filename: Filename of the CSS source file if it is necessary. If not specified, the PDF will be plain HTML.
  • function md_to_html_email(source_md, source_css): Creates an HTML email out of markdown source with optional CSS formatting. This function will create a Python email object with a MIME HTML attachment as well as a plaintext attachment containing the original Markdown, as a fallback. returns a Python MIMEMultipart object.
    • source_md: Markdown source to put in the email.
    • source_css: CSS to apply to the HTML document. This will be embedded into the page. Optional. If not specified, there will be no CSS.
  • function md_to_html_document: Takes Markdown and turns it into a full HTML document with a head and body.
    • source_md: Markdown source to turn into a document.
    • source_css: Optional CSS to format the document with.
  • function pdf_file_to_pdf_attachment(source_pdf_filename): Creates a Python MIMEBase object out of an existing PDF file for attaching to emails. This object will be sent as a PDF attachment. returns a Python MIMEBase object.
  • function get_html_head(title, source_css): Creates an HTML head with any CSS.
    • title: Optional title for the HTML document
    • source_css: Optional CSS to put into the HTML head
Clone this wiki locally