[TxMt] Re: New Bundle

Allan Odgaard throw-away-1 at macromates.com
Thu Jan 26 13:23:53 UTC 2006


On 24/1/2006, at 21:26, Oliver Taylor wrote:

>> I would suggest using htmldoc [1] for the HTML -> PDF conversion.
> I'll check it out.

It's easy to install with Darwin Ports (sudo port install htmldoc)  
but it seems that htmldoc do not use the documents style sheet, at  
least not in the DP version (and with the switches I used).

Prince seems to do, judging from Ben Jacksons blog: http:// 
www.unfitforprint.com/articles/2005/12/23/markdown2pdf-with-prince- 
xml-and-textmate

> [...]
> s/^[A-Z].*:\s*/<h4>$&<\/h4>/; # transition (left)
>
> The only one that's not working properly is the last one. It's  
> baffling to be because it's the same regexp as in the language.

I think because your transitions are right-aligned? The ^ character  
is a “begin of line anchor”. So you would need to add \s+ after it  
(for 1 or more whitespaces)

> [...]
> There are a few steps left in the process that need to be  
> addressed. Next the HTML marked-up text (as generated by the above  
> script) needs to be inserted into an actual HTML document with  
> doctype declarations, CSS etc. And somehow (again, I have no idea  
> how) it needs to be transfered to a PDF authoring environment  
> (htmldoc or whatever).

I changed/added to the command to generate a full HTML document, open  
it in Safari, and then also convert it to PDF and open with Preview.

Change the output to HTML (or maybe discard).

It stores the resulting files in /tmp (using the basename of your  
current document).

In practice you could use Safari to “Print to PDF”, but that's  
tedious, if you do it a lot.

----------8<----------

# first figure out a name for the result
NAME="${TM_FILENAME:-untitled}"
BASENAME="${NAME%.*}"
DST="/tmp/$BASENAME"

# everything we output within { … } is written to the HTML file via  
redirection (see line with the })
{
# first output HTML header
cat <<HEAD
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">

<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<title>$BASENAME</title>
	<style type="text/css" media="screen">
	/* The styles listed here can be static, but these are no where near  
a final reference, hust a sample. */
		body {
			font-family: Courier;
			font-size: 12pt ;
		}
		#contentwrapper {
			width: 6in;
			margin: 1in 1in 1in 1.5in;
		}
		dl {
			margin: 0 1.5in 0 1.5in;
			padding: 0;
		}
		dt {
			margin: 1em 0 0 1in;
			padding: 0;
		}
		dd {
			margin: 0;
			padding: 0;
		}
		dd.parenthetical {
			margin-left: .5in ;
			width: 1.5in;
			padding: 0;
		}
		h1,h2,h3,h4,h5,h6 {
			font-weight: normal;
			font-size: 12pt;
			margin-top: 1em;
		}
		h3 {
			text-align: right;
			margin: 0 1in 0 2in;
		}
	</style>
</head>
<body>
<div id="contentwrapper">
HEAD

# then HTML body (converted from document)
perl -pe '
	s/&/&/g; #ampersands
	s/</</g; #reserved for HTML
	s/>/&lgt;/g; #reserved for HTML - maybe this is unnecessary?
	s/^EXT\..*$/<h2>$&<\/h2>/; #scene heading
	s/^INT\..*$/<h2>$&<\/h2>/; #scene heading
	s/^I\/E\..*$/<h2>$&<\/h2>/; #scene heading
	s/^[A-Z].*\-\s[A-Z].*/<h2>$&<\/h2>/; #arbitrary scene heading ending  
with a time
	s/^[A-Z].*\-\s*$/<h2>$&<\/h2>/; #arbitrary scene heading NOT ending  
with a time
	s/^\w.*$/<p>$&<\/p>/; #paragraph
	s/\/\/(.*)\/\//<!-- $1 -->/g; #comments
	s/\*(.*)\*/<em>$1<\/em>/;	#italics
	s/^(\t{4})([^\t].*)$/<dl>$1<dt>$2<\/dt>/; #characters
	s/^(\t{3})([^\t].*)$/$1<dd class="parenthetical"> $2 <\/dd>/;  
#parenthetical
	s/^(\t{2})([^\t].*)$/$1<dd>$2<\/dd><\/dl>/; #dialogue
	s/^(\t{10})([^\t].*:)$/$1<h3>$2<\/h3>/; #transition (right)
	s/^\s+[A-Z].*:\s*/<h4>$&<\/h4>/; # transition (left)
'

# and finally HTML footer
cat <<'TAIL'
</div> <!-- contentwrapper -->
</body>
</html>
TAIL
} >"$DST.html"

# open the generated HTML file in Safari
open -a Safari "$DST.html"

# convert to PDF with htmldoc and open in Preview
require_cmd htmldoc
htmldoc -f "$DST.pdf" --header "" --footer "" --webpage "$DST.html"|pre
open -a Preview "$DST.pdf"




More information about the textmate mailing list