Ich habe es geschafft. Ich bin nicht ganz glücklich über meine Lösung, aber sie ist anständig genug. Warten auf eine andere Person, um eine bessere Lösung zu finden.
Der Prozess folgt. Konvertieren Sie den Markdown in HTML und hängen Sie ihn an die Nachricht an. Drehen Sie diesen Anhang zu inline
Anhang. Aber jetzt habe ich zwei Anhänge, der erste ist in Markdown und der zweite ist HTML. Ersetzen Sie den Markdown-Inhalt durch eine leere Zeichenfolge, damit nur HTML gesendet wird.
Ich habe der ~/.muttrc
Datei die folgende Zeile hinzugefügt .
macro compose B ":set editor=text2mime-markdown.py<enter>E:set editor=email-editor<enter>Da/tmp/html-markdown-alternative.html<enter>^Du"
Hier ist, email-editor
was von dem Link in Frage gestellt entlehnt ist.
#!/bin/sh
if grep -q In-Reply-To $1; then
# Jump to first line of message
exec vim -c 'norm }j' $1
else
# Enter insert mode on the To: line
exec vim $1
fi
Und die Haupt-Python-Datei, die aufgerufen wird, folgt. Dies ist inspiriert vom Perl-Skript des betreffenden Links.
#!/usr/bin/env python
import os
import sys
from formatter import *
version = "0.1"
file = sys.argv[1]
new_file = "/tmp/html-markdown-alternative.html"
with open(file, "r") as f:
text = f.read()
lines = text.split('\n')
header = []
body = []
headerStart = True
for l in lines:
if headerStart:
m = re.search(r'^[\w\-]+\:', l)
if m:
header.append(l)
else:
headerStart = False
body.append(l)
else:
body.append(l)
header = '\n'.join(header)
body = '\n'.join(body)
htmlBody = markdownToHtml(body);
html = []
html.append('<html>')
html.append('<head>')
html.append('<meta name=\"generator\" content=\"text2mime-markdown{}\">'.format(version))
html.append('<style>')
html.append("code { font-family: 'Andale Mono', 'Lucida Console', \
'Bitstream Vera Sans Mono', 'Courier New', monospace; }")
html.append('pre { border-left: 20px solid #ddd; margin-left: 10px; \
padding-left: 5px; }')
html.append('</style>')
html.append('</head>')
html.append('<body>')
html.append('{0}'.format(body))
html.append('</body>')
html.append('</html>')
html = '\n'.join(html)
with open(new_file, "w") as newF:
newF.write(html)
with open(file, 'w') as f:
f.write(header)
Dies hängt von einer weiteren Python-Datei ab, formatter.py
die pandoc
zum Formatieren meiner E-Mails verwendet wird. Wenn sie pandoc
nicht verfügbar ist, kann sie python-markdown2
package verwenden. Dieses Skript folgt.
import subprocess
import re
import os
import sys
import html2text
import collections
# check if pandoc exists
panDoc = True
try:
subprocess.call(["pandoc", '--version']
, stdout=subprocess.PIPE
, stdin=subprocess.PIPE
)
except OSError:
panDoc = False
if not panDoc:
import text.html2text as html2text
import markdown
def decodeText(text):
return text.decode('utf-8')
def markdownToHtml(content, convertor='pandoc'):
global panDoc
if panDoc:
cmd = ["pandoc", "-f", "markdown", "-t", "html"]
p = subprocess.Popen(cmd
, stdin = subprocess.PIPE
, stdout = subprocess.PIPE
)
p.stdin.write(content)
content = p.communicate()[0]
return decodeText(content)
else:
return markdown.markdown(decodeText(content))
def htmlToMarkdown(content, convertor='pandoc'):
global panDoc
if panDoc and convertor == 'pandoc':
cmd = ["pandoc", "-t", "markdown", "-f", "html"]
p = subprocess.Popen(cmd
, stdin = subprocess.PIPE
, stdout = subprocess.PIPE
)
p.stdin.write(content)
content = p.communicate()[0]
return decodeText(content)
# Use markdown package to convert markdown to html
else:
h = html2text.HTML2Text()
content = h.handle(decodeText(content))
return content
def titleToBlogDir(title):
if title is None:
return ''
if len(title.strip()) == 0:
return ''
blogDir = title.replace(" ","_").replace(':', '-').replace('(', '')
blogDir = blogDir.replace('/', '').replace('\\', '').replace('`', '')
blogDir = blogDir.replace(')', '').replace("'", '').replace('"', '')
return blogDir
def titleToFilePath(title, blogDir):
if len(blogDir.strip()) == 0:
return ''
fileName = os.path.join(blogDir, titleToBlogDir(title))
return fileName
def htmlToHtml(html):
return decodeText(html)
def metadataDict(txt):
mdict = collections.defaultdict(list)
md = getMetadata(txt)
for c in ["title", 'type', "layout", "status", "id", "published", "category", "tag"]:
pat = re.compile(r'{0}\:\s*(?P<name>.+)'.format(c), re.IGNORECASE)
m = pat.findall(txt)
for i in m:
mdict[c].append(i)
return mdict
def getMetadata(txt):
"""
Get metadata out of a txt
"""
if not "---" in txt:
print txt
sys.exit(1)
pat = re.compile(r'\-\-\-+(?P<metadata>.+?)\-\-\-+', re.DOTALL)
m = pat.search(txt)
if m:
return m.group('metadata')
else:
sys.exit(0)
def getContent(txt):
"""
Return only text of the post.
"""
pat = re.compile(r'\-\-\-+(?P<metadata>.+?)\-\-\-+', re.DOTALL)
return re.sub(pat, "", txt)
def readInputFile(fileName):
"""
read file and return its format. html or markdown
"""
assert fileName
if not os.path.exists(fileName):
raise IOError, "File %s does not exists" % fileName
# Check the fmt of file.
fmt = os.path.splitext(fileName)[1].lower()
if fmt in ["htm", "html", "xhtml"]:
fmt = "html"
elif fmt in ["md", "markdown"]:
fmt = "markdown"
else:
fmt = "markdown"
txt = open(fileName, 'r').read()
return (fmt, txt)
def formatContent(txt, fmt):
"""
Format the content as per fmt.
"""
content = getContent(txt)
if fmt == "html":
content = htmlToHtml(content)
elif fmt == "markdown":
content = markdownToHtml(content)
else:
content = markdownToHtml(content)
return content
Diese Dateien sind auch hier verfügbar: https://github.com/dilawar/mutt
sendmail
?