diff options
author | Luke Shumaker <LukeShu@sbcglobal.net> | 2011-08-28 23:12:10 -0400 |
---|---|---|
committer | Luke Shumaker <LukeShu@sbcglobal.net> | 2011-08-28 23:12:10 -0400 |
commit | f24e77857c4af0c53ac4c7db7e671c3fbf9dc67b (patch) | |
tree | 4e3f0fe3f6d0c2ab7492a06b82918d879eb7b70c |
A neat little weekend (well, two Fridays and a Sunday) project to 1) generate org-mode files from copy/pasted data from MS-word 2) generate Wordpress posts from such org-mode files
-rw-r--r-- | make-post.php | 115 | ||||
-rw-r--r-- | upload-msword.php | 31 | ||||
-rw-r--r-- | upload-org.php | 18 | ||||
-rwxr-xr-x | word2org.sh | 32 |
4 files changed, 196 insertions, 0 deletions
diff --git a/make-post.php b/make-post.php new file mode 100644 index 0000000..200f853 --- /dev/null +++ b/make-post.php @@ -0,0 +1,115 @@ +<?php +header('Content-type: text/html; charset=utf-8'); +$dir = '/home/luke/j1-uploads'; + +if (!isset($_POST['filename'])) { + echo '<form method="post" action="make-post.php">'; + echo '<ul>'; + if ($handle = opendir($dir)) { + $id=0; + while (false !== ($file = readdir($handle))) { + $filename = htmlentities(rawurldecode($file)); + echo '<li>'; + echo "<input type='radio' name='filename' value='$file' id='$id' />"; + echo "<label for='$id'>$filename</label>"; + echo '</li>'; + $id++; + } + } + echo '</ul>'; + echo '<input type="submit" value="submit" />'; + echo '</form>'; + exit(); +} + +$filename = $dir.'/'.rawurlencode($_POST['filename']); +$lines = explode("\n", file_get_contents($filename)); + +$reporters = array(); +$reporter = ''; +$cite = ''; +foreach ($lines as $line) { + preg_match('/^(\**) +(.*)/', $line, $matches); + $base = $matches[1]; + $content = $matches[2]; + + switch ($base) { + case '*': break; + case '**': + $reporter = $content; + $reporters[$reporter] = array(); + break; + case '***': + $cite = $content; + break; + case '': + $reporters[$reporter][] = + array('cite'=>$cite, + 'text'=>$content); + break; + } +} + +if (!isset($_POST['q'])) { + echo '<form method="post" action="make-post.php">'; + echo '<input type="hidden" name="filename" value="'.htmlentities($_POST['filename']).'" />'; + echo '<textarea name="open"></textarea>'; + $i = 0; + foreach ($reporters as $name => $quotes) { + if (count($quotes)>1) { + echo '<fieldset>'; + echo '<legend>'.$name.'</legend>'; + echo '<ul>'; + + foreach ($quotes as $key => $quote) { + echo '<li>'; + echo '<input '. + 'type="radio" '. + "name='q[$name]' ". + "value='$key' ". + "id='id$i' ". + '/>'; + echo "<label for='id$i'>"; + + echo "<blockquote>"; + echo $quote['text']; + echo "<footer>— <cite>".$quote['cite']."</cite></footer></blockquote>"; + + echo '</label>'; + echo '</li>'; + } + } else { + foreach ($quotes as $key => $quote) { + echo '<input '. + 'type="hidden" '. + "name='q[$name]' ". + "value='$key' ". + '/>'; + break; + } + } + echo '</ul>'; + echo '</fieldset>'; + } + echo '<input type="submit" value="submit" />'; + echo '</form>'; +} else { + $quotes = array(); + foreach ($_POST['q'] as $reporter => $quote_id) { + $quote = $reporters[$reporter][$quote_id]; + $quote['reporter'] = $reporter; + $quotes[] = $quote; + } + + echo htmlentities($_POST['open']); + echo "\n<!--more-->\n"; + $reporter_names = array(); + foreach ($quotes as $quote) { + $reporter_names[] = $quote['reporter']; + echo "<blockquote>"; + echo $quote['text']; + echo "<footer>— <cite>".$quote['cite']."</cite></footer></blockquote>\n"; + } + $last_reporter = array_pop($reporter_names); + echo "\nQuotes gathered by ".(count($reporter_names)>0?implode(', ', $reporter_names).' and ':'').$last_reporter.'.'; +} diff --git a/upload-msword.php b/upload-msword.php new file mode 100644 index 0000000..b234381 --- /dev/null +++ b/upload-msword.php @@ -0,0 +1,31 @@ +<?php +header('Content-type: text/html; charset=utf-8'); +$dir = '/home/luke/j1-uploads'; + +$set = isset($_POST['word_data']) && isset($_POST['filename']); +if ($set) { + $tmp = $dir.'/'.rawurlencode($_POST['filename']).'.tmp'; + $fd = fopen($tmp, 'w'); + fwrite($fd, $_POST['word_data']); + fclose($fd); + + $filename = $dir.'/'.rawurlencode($_POST['filename']).'.org'; + $cmd = "< '$tmp' dos2unix | ".dirname(__FILE__)."/word2org.sh 2>&1 > '$filename'"; + echo '<pre>'.htmlentities($cmd).'</pre>'; + echo '<pre>'; + system($cmd, $status); + echo '</pre>'; + unlink($tmp); + if ($status != 0) { + unlink($filename); + echo '<p>error</p>'; + } +} else { + ?> + <form method="post" action="upload-msword.php"> + <input type="text" name="filename" /> + <textarea name="word_data"></textarea> + <input type="submit" value="upload" /> + </form> + <?php +}
\ No newline at end of file diff --git a/upload-org.php b/upload-org.php new file mode 100644 index 0000000..40f403f --- /dev/null +++ b/upload-org.php @@ -0,0 +1,18 @@ +<?php +$dir = '/home/luke/j1-uploads/'; + +$set = isset($_POST['org_data']) && isset($_POST['filename']); +if ($set) { + $filename = $dir.'/'.rawurlencode($_POST['filename']).'.org'; + $fd = fopen($filename, 'w'); + fwrite($fd, $_POST['word_data']); + fclose($fd); + echo 'uploaded'; +} else { + ?> + <form method="post" action="upload-msword.php"> + <input type="text" name="filename" /> + <textarea name="org_data"></textarea> + </form> + <?php +} diff --git a/word2org.sh b/word2org.sh new file mode 100755 index 0000000..2aff59f --- /dev/null +++ b/word2org.sh @@ -0,0 +1,32 @@ +#!/bin/bash +export LANG='en_US.UTF-8' +quote='' +quote_credit='' +block='' + +out=$1 + +while read line; do + case "$line" in + '“'*) # quote + line="`echo "$line"|sed 's/[“”]//g'`" + quote="$quote $line" + :;; + '-'*) # quote-credit + credit="`echo "$line"|sed 's/^-//'`" + block="$block +*** $credit + $quote" + quote='' + :;; + 'Reporter: '*) # reporter-credit + reporter="`echo "$line"|sed 's/^Reporter: //'`" + echo "** $reporter$block" + block='' + :;; + '') :;; # blank + *) # error + echo "error: \`$line'" >>/dev/stderr + exit 1;; + esac +done |