Some times ago, I suggest the possibility to use templates with txt2tags
http://sourceforge.net/mailarchive/message.php?msg_name=499BDB6F.4000204%40cmi.univ-mrs.fr
As an attachment, here is a simple patch (against last svn) for this. It implements the specification T3, as it was called in my old post.

Basically, it adds an option to select a file, where there is a %(BODY)s macro that will be replaced that was is processed from the t2t markup.
This is way easier to tweak the default templates than using postprocs.
Just a warning for the LaTeX target, due to its special use in python, always double the %...

You can also use txt2tags macros for free in the template. Here is an example

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META NAME="generator" CONTENT="http://txt2tags.sf.net";>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=%(ENCODING)s">

<!-- A lot of META, robot, IE hacks can be added -->

<LINK REL="stylesheet" TYPE="text/css" HREF="%(STYLE)s">
<TITLE>%(HEADER1)s</TITLE>
</HEAD>
<BODY BGCOLOR="white" TEXT="black">

<IMG SRC="logo.png"></IMG>

<!-- html code for a nav bar -->

<P ALIGN="center"><CENTER><H1>%(HEADER1)s</H1>
<FONT SIZE="4">
<I>%(HEADER2)s</I><BR>
%(HEADER3)s
</FONT></CENTER>

%(BODY)s

Last Modified on %%MTIME(%A %d %B).
(<a href="%%INFILE">source</a>)
</BODY>


Save it as template.html, then you go
txt2tags -T template -t html myfile.t2t

I have been using it for some times, and "it seems to work".
If the audience would like to share its thoughts on it...

Regards,
Emmanuel
=== modified file 'txt2tags'
--- txt2tags	2009-04-07 20:05:55 +0000
+++ txt2tags	2010-03-02 19:53:47 +0000
@@ -148,7 +148,7 @@
 OPTIONS  = {'target'     :'', 'toc-level'  :3 , 'style'      :'',
             'infile'     :'', 'outfile'    :'', 'encoding'   :'',
             'config-file':'', 'split'      :0 , 'lang'       :'',
-            'show-config-value':'', 'ascii-art' :''} 
+            'show-config-value':'', 'ascii-art' :'', 'template':''} 
 ACTIONS  = {'help'       :0 , 'version'    :0 , 'gui'        :0 ,
             'verbose'    :0 , 'debug'      :0 , 'dump-config':0 ,
             'dump-source':0 }
@@ -222,6 +222,7 @@
 '                      %s'  % ', '.join(TARGETS[8:]),
 _("  -i, --infile=FILE   set FILE as the input file name ('-' for STDIN)"),
 _("  -o, --outfile=FILE  set FILE as the output file name ('-' for STDOUT)"),
+_("  -T, --template=NAME use file NAME.<target> as template"),
 _("  -H, --no-headers    suppress header, title and footer contents"),
 _("      --headers       show header, title and footer contents (default ON)"),
 _("      --encoding=ENC  set target file encoding (utf-8, iso-8859-1, etc)"),
@@ -2159,6 +2160,7 @@
 			'o':'outfile',
 			'q':'quiet',
 			't':'target',
+			'T':'template',
 			'v':'verbose',
 			'V':'version',
 		}
@@ -4130,17 +4132,30 @@
 
 
 def doHeader(headers, config):
-	if not config['headers']: return []
+	if not config['headers']: return config['fullBody']
 	if not headers: headers = ['','','']
 	target = config['target']
 	if not HEADER_TEMPLATE.has_key(target):
 		Error("doHeader: Unknown target '%s'"%target)
 	
-	if target in ('html','xhtml') and config.get('css-sugar'):
-		template = HEADER_TEMPLATE[target+'css'].split('\n')
-	else:
-		template = HEADER_TEMPLATE[target].split('\n')
-	
+	if config['template'] == '' : # use default templates
+		if target in ('html','xhtml') and config.get('css-sugar'):
+			template = HEADER_TEMPLATE[target+'css'].split('\n')
+		else:
+			template = HEADER_TEMPLATE[target].split('\n')
+		template.append("\n%(BODY)s\n")
+		template.append(TAGS['EOD'])
+	else: # read user's template file
+		templatefile = ''
+		names = [config['template']+'.'+target,config['template']]
+		for file in names:
+			if os.path.isfile(file): 
+				templatefile = file 
+				break
+		if not templatefile:
+			Error(_("Cannot find template file:")+config['template'])
+		template = Readfile(templatefile,remove_linebreaks=1)
+		
 	head_data = {'STYLE':[], 'ENCODING':''}
 	for key in head_data.keys():
 		val = config.get(key.lower())
@@ -4202,7 +4217,11 @@
 					template.insert(i+1, template[i].replace(style_mark, styles.pop()))
 				del template[i]
 				break
+	# Expand macros on *all* lines of the template
+	template = map(MacroMaster(config=config).expand,template)
 	# Populate template with data (dict expansion)
+	# Adding Body contents
+	head_data['BODY'] = '\n'.join(config['fullBody'])
 	template = '\n'.join(template) % head_data
 	
 	# Adding CSS contents into template (for --css-inside)
@@ -4260,7 +4279,6 @@
 	ret.append('')
 	ret.append(doCommentLine(ppgd))
 	ret.append(doCommentLine(cmdline))
-	ret.append(TAGS['EOD'])
 	return ret
 
 def doEscape(target,txt):
@@ -4596,7 +4614,6 @@
 		#TODO escape line before?
 		#TODO see exceptions by tex and mgp
 		Message(_("Composing target Headers"),1)
-		target_head = doHeader(source_head, myconf)
 		# Parse the full marked body into tagged target
 		first_body_line = (len(source_head) or 1)+ len(source_conf) + 1
 		Message(_("Composing target Body"),1)
@@ -4617,7 +4634,8 @@
 		if TARGET not in ['txt', 'art']: 
 			target_foot = doFooter(myconf)
 		# Finally, we have our document
-		outlist = target_head + target_toc + target_body + target_foot
+		myconf['fullBody'] = target_toc + target_body + target_foot
+		outlist = doHeader(source_head, myconf)
 		# If on GUI, abort before finish_him
 		# If module, return finish_him as list
 		# Else, write results to file or STDOUT

Title: %(HEADER1)s

%(HEADER1)s

%(HEADER2)s
%(HEADER3)s
%(BODY)s Last Modified on %%MTIME(%A %d %B). (source)
------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
txt2tags-list mailing list
https://lists.sourceforge.net/lists/listinfo/txt2tags-list

Reply via email to