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