User Tools

Site Tools


dokuwiki_markdown_table_converter

Dokuwiki Markdown Table Converter

I often seemed to be manually converting tables into markdown so I wrote a quick script to convert copy and pasted comma (or tab) seperated tables into dokuwiki table markdown.

You can try it out at this URL: https://starflyer.armchairscientist.co.uk/markdown_converter/

Source code

markdown.php
<html>
<head>
<title>Markdown Converter</title>
<style type='text/css'>
textarea { width: 100%; }
</style>
<script>
function copy_to_clipboard(id) {
    document.getElementById(id).select();
    document.execCommand('copy');
}
</script>
</head>
<body>
<h1>Table to Markdown Convertor</h1>
<h2>Paste your table</h2>
<form method='post'>
<textarea rows='10' cols='30' name='data'></textarea>
Seperated by:
<input type='radio' value='Tab' name='csv_type' <?php if(empty($_REQUEST['csv_type']) or $_REQUEST['csv_type'] == 'Tab') echo " checked "; ?> /> Tab
<input type='radio' value='Comma' name='csv_type' <?php if(!empty($_REQUEST['csv_type']) and $_REQUEST['csv_type'] == "Comma") echo " checked "; ?> /> Comma
<input type='submit' value='Convert to Markdown' name='go' />
<input type='checkbox' value='header_row' name='header_row' /> Contains header row
</form>
<h2>Markdown output</h2>
<input type="button" value="Copy to clipboard" onclick="copy_to_clipboard('output');">
<textarea rows='20' cols='30' id='output'>
<?php
if(!empty($_REQUEST['data'])) {
   $data = explode("\n", $_REQUEST['data']);
   $seperator = "\t";
   if(!empty($_REQUEST['csv_type']) and $_REQUEST['csv_type'] == 'Comma')
      $seperator = ",";
 
   $output = "";
   if(!empty($_REQUEST['header_row'])) {
      $data[0] = str_replace("\r", "", $data[0]);
      $output .= "^".str_replace($seperator, "^", $data[0])."^\n";
      unset($data[0]);
   }
 
   if(is_array($data)) {
      foreach($data as $line) {
         $stripped_line = str_replace("\r", "", $line);
         $output .= "|".str_replace($seperator, "|", $stripped_line)."|\n";
      }
   }
}
echo $output;
?></textarea>
</body>
</html>
dokuwiki_markdown_table_converter.txt · Last modified: 2020/03/05 23:03 by admin