PageRenderTime 34ms CodeModel.GetById 12ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/extras/txt2tags.php

http://txt2tags.googlecode.com/
PHP | 261 lines | 173 code | 44 blank | 44 comment | 19 complexity | 0d5be4b524ef5aeb1e13fcc79c5aeed3 MD5 | raw file
  1<? # txt2tags.php (http://txt2tags.org)
  2   # A handy web interface for the txt2tags conversion tool
  3   # License: GPL
  4   #
  5   # 2002-03-22 Aurelio Jargas <verde@aurelio.net>
  6   # 2004-06-26 Aurelio: v2.0: %%date, photo, s/orange/blue/, XHTML target,
  7   #            CSS, work in new PHP, --enum-title, show parsed x?html
  8   # 2004-10-20 Aurelio: v2.1: Text capitalized, using button instead photo
  9   # 2004-12-28 Aurelio: v2.2: Lout target
 10   # 2005-05-28 Aurelio: v2.3: link: s/sample.t2t/markup.html/, gray table
 11   # 2006-07-24 Aurelio: v2.4: rewrote. Now configurable, clean and modular
 12   #            Form using labels and fieldset, --toc, hints simplified
 13   # 2008-07-08 Aurelio: v2.5: is_standalone flag to turn headers on/off
 14   # 2010-10-22 Aurelio: v2.6: new targets, $dfttarget, targets alpha order
 15
 16
 17#-----------------------------[ CONFIG ]-----------------------------
 18#
 19# The txt2tags program location (PATH)
 20#
 21$prog = "./txt2tags";
 22
 23# Set to 1 to use this file alone (will print page headers)
 24# Set to 0 to use this file inserted on another .php (embedded)
 25$is_standalone = 1;
 26
 27# The default target
 28$dtftarget = "html";
 29
 30# The default text encoding
 31$dtfencoding = "UTF-8";
 32
 33# The default marked text
 34#
 35$dfttxt = "= My Title =";
 36$dfttxt.= "\nA __paragraph__ with **bold** and //italic//.\n";
 37$dfttxt.= "\nToday is %%date(%c).";
 38$dfttxt.= "\nHere is a nice pic: [img/t2tpowered.png].\n";
 39$dfttxt.= "\n  | John |  33  |    Male |";
 40$dfttxt.= "\n  | Mary |  19  |  Female |";
 41
 42# The options labels for the form
 43#
 44$labels = array(
 45	'target'    => 'Target',
 46	'noheaders' => 'Hide headers',
 47	'enumtitle' => 'Numbered titles',
 48	'toc'       => 'Table Of Contents',
 49	'csssugar'  => 'CSS Sugar (HTML and XHTML)'
 50);
 51
 52# The available targets
 53#
 54$targets = array(
 55	'art'    => 'ASCII Art text',
 56	'adoc'   => 'AsciiDoc document',
 57	'creole' => 'Creole 1.0 document',
 58	'dbk'    => 'DocBook document',
 59	'doku'   => 'DokuWiki page',
 60	'gwiki'  => 'Google Wiki page',
 61	'html'   => 'HTML page',
 62	'tex'    => 'LaTeX document',
 63	'lout'   => 'Lout document',
 64	'mgp'    => 'MagicPoint slides',
 65	'moin'   => 'MoinMoin page',
 66	'pm6'    => 'PageMaker document',
 67	'txt'    => 'Plain Text (no marks)',
 68	'pmw'    => 'PmWiki page',
 69	'sgml'   => 'SGML document',
 70	'man'    => 'UNIX Manual page',
 71	'wiki'   => 'Wikipedia page',
 72	'xhtml'  => 'XHTML page'
 73);
 74
 75
 76#----------------------------[ FUNCTIONS ]---------------------------
 77
 78function FormSelect($name, $arr, $selected='', $size=0) {
 79	if ($size == 0) $size = count($arr);
 80	$r = "\n<select id=\"$name\" name=\"$name\" size=\"$size\">\n";
 81	while (list($id, $txt) = each($arr)) {
 82		$sel = ($id == $selected) ? ' selected' : '';
 83		$r .= "<option value=\"$id\"$sel>$txt</option>\n";
 84	}
 85	$r .= "</select>\n";
 86	return $r;
 87}
 88
 89function FormCheck($name, $value, $on=0){
 90	$on = ($on) ? 'checked' : '';
 91	return "<input type=\"checkbox\" id=\"$name\" name=\"$name\" value=\"$value\" $on>";
 92}
 93
 94function FormLabel($name) {
 95	global $labels;
 96	return "<label for=\"$name\">{$labels[$name]}</label>";
 97	}
 98
 99function getvar($name){
100	eval('global $'.$name.';');         # first try the global one
101	eval('$val = $'.$name.';');
102	if (!$val) $val = $_SERVER[$name];  # if not found, try others
103	if (!$val) $val = $_POST[$name];
104	# echo "<p>key: <b>$name</b>, value: <b>$val</b>---</p>"; 
105	return $val;
106}
107
108
109#-----------------------------[ INIT ]----------------------------
110
111$myself    = getvar('PHP_SELF');
112$txt       = getvar('txt');
113$target    = getvar('target');
114$noheaders = getvar('noheaders');
115$enumtitle = getvar('enumtitle');
116$toc       = getvar('toc');
117$csssugar  = getvar('csssugar');
118
119if (!$txt) $txt = $dfttxt;
120if (!$target) $noheaders = '-H';    # Default ON
121
122
123#---------------------------[ HEADERS ]----------------------------
124
125if ($is_standalone) {
126
127?>
128
129<html>
130<head><title>txt2tags // ONE source, MULTI targets</title>
131
132<style type="text/css">
133	body { margin:20px; padding:0; }
134	h1 { border-bottom:1px solid black; }
135	form { margin-top:3em; font-size:85%; }
136	fieldset { background:#ffc; margin:0 0 1em 0; border:1px solid #ddd; width:40em; }
137	textarea { font-size:90%; }
138	table#markuphint { float:right; background:#ffa; border:1px solid #ccc; font-size:90%; }
139	table#markuphint tr { line-height:100%; }
140	table#markuphint td { padding:0 7px; }
141	pre#output { margin-left:3em; overflow:auto; }
142	#parsed { border:1px solid #999; margin:0 2em 2em 3em; padding:1em; }
143	#parsed td { border-style:solid; }
144	#footer { border-top:1px solid black; }
145</style>
146
147</head>
148<body>
149
150<h1>txt2tags WEB Interface</h1>
151
152<?
153}
154
155#------------------------------[ FORM ]------------------------------
156?>
157<form id="userinput" method="post" action="<? $myself ?>">
158
159<fieldset>
160	<legend>Text Source</legend>
161	<textarea name="txt" rows="8" cols="53"><? echo $txt ?></textarea>
162</fieldset>
163<br>
164<fieldset>
165	<legend>Options</legend>
166
167	<table id="markuphint">
168	<caption>Markup Hints</caption>
169	<tr>
170		<td>**bold**</td>
171		<td>= title =</td>
172		<td>%%date</td>
173	</tr><tr>
174		<td>//italic//</td>
175		<td>- list</td>
176		<td>[image.jpg]</td>
177	</tr><tr>
178		<td>__under__</td>
179		<td>+ numlist</td>
180		<td colspan="2">[link www.com]</td>
181	</tr><tr>
182		<td>--strike--</td>
183		<td>``code``</td>
184		<td>| table |</td>
185	</tr>
186	</table>	
187	
188	<?
189	echo FormLabel('target').FormSelect('target', $targets, $target ? $target : $dtftarget, 1);
190	echo '<br><br>';
191	echo FormCheck('noheaders', '--no-headers', $noheaders);
192	echo FormLabel('noheaders').'<br>';
193	echo FormCheck('enumtitle', '-n', $enumtitle);
194	echo FormLabel('enumtitle').'<br>';
195	echo FormCheck('toc', '--toc', $toc);
196	echo FormLabel('toc').'<br>';
197# Mmmmm, no.
198#	echo FormCheck('csssugar', '--css-sugar', $csssugar);
199#	echo FormLabel('csssugar').'<br>';
200	?>
201</fieldset>
202<br>
203<input type="submit" value="Convert!">
204</form>
205   
206<?
207#----------------------------[ PROCESSING ]--------------------------
208
209if ($target){
210
211	# Always empty headers on input
212	$txt = escapeshellarg("\n$txt");
213	
214	# Compose command line
215	$cmd = array();
216	if ($noheaders) $cmd[] = $noheaders;
217	if ($enumtitle) $cmd[] = $enumtitle;
218	if ($toc      ) $cmd[] = $toc;
219	if ($csssugar ) $cmd[] = $csssugar;
220	$cmd[] = "--encoding $dtfencoding";
221	$cmd[] = "-t $target";
222	$cmd[] = "-i -";
223	$cmd[] = "-o -";
224	
225	# Security: Remove symbols from command line
226	$cmd = ereg_replace('[^A-Za-z0-9 -]', '', implode(' ', $cmd));
227
228	# Show results
229	?>	
230	<h3>Text converted to <? echo strtoupper($target) ?></h3>
231
232	<pre id="output"><? echo htmlspecialchars(`echo $txt | $prog $cmd`) ?>
233	</pre>
234	
235	<?
236	if ($target == 'html' || $target == 'xhtml') {
237	?>
238		<h3><? echo strtoupper($target) ?> parsed</h3>
239
240		<div id="parsed">
241		<? echo `echo $txt | $prog -H $cmd` ?>
242		</div>
243	<?
244	}
245}
246
247#----------------------------[ FOOTER ]--------------------------
248
249if ($is_standalone) {
250?>
251
252<p id="footer">
253Txt2tags site: <a href="http://txt2tags.org">http://txt2tags.org</a><br>
254Author: <a href="http://aurelio.net/en/">Aurelio Jargas</a>
255</p>
256
257</body>
258</html>
259<?
260}
261?>