Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkLodato committed Dec 10, 2010
0 parents commit b2e2169
Show file tree
Hide file tree
Showing 2 changed files with 256 additions and 0 deletions.
162 changes: 162 additions & 0 deletions Aafigure.php
@@ -0,0 +1,162 @@
<?php /*
MediaWiki Aafigure extension
See README for documentation.
Copyright (c) 2010, Mark Lodato <lodatom@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

class AafigureSettings {
public $aafigure, $format;
};

$wgAafigureSettings = new AafigureSettings();
$wgAafigureSettings->aafigure = 'aafigure';
$wgAafigureSettings->format = 'png';
$wgAafigureSettings->allowed_formats = array();

$wgExtensionFunctions[] = "wfAafigureTag";

$wgExtensionCredits['parserhook'][] = array(
'path' => __FILE__,
'name'=>'Aafigure',
'author'=>'[mailto:lodatom@gmail.com Mark Lodato]',
'url'=> 'http://www.mediawiki.org/wiki/Extension:Aafigure',
'description'=>'Parse ASCII Art images in "aafigure".',
'version'=>'1.0'
);

function wfAafigureTag() {
global $wgParser;
$wgParser->setHook( "aafigure", "renderAafigure" );
}

function renderAafigure( $input, $args, $parser )
{
global $wgUploadDirectory, $wgUploadPath, $wgAafigureSettings;

$subdir = '/aafigure/';
$dir = $wgUploadDirectory . $subdir;

$format = $wgAafigureSettings->format;
$options = '';
$width = 0;
$height = 0;
foreach ($args as $name => $value) {
switch ($name) {
case 'format':
if ( $value != $format ) {
$value = strtolower( $value );
$allowed = $wgAafigureSettings->allowed_formats;
if ( ! ( in_array( $value, $allowed ) ) ) {
$error = 'allowed formats: ' . implode(', ', $allowed);
break 2;
}
$format = $value;
$options .= ' --type=' . escapeshellarg($format);
}
break;
case 'width':
$width = intval( $value );
break;
case 'height':
$height = intval( $value );
break;
case 'aspect':
case 'background':
case 'fill':
case 'foreground':
case 'linewidth':
case 'scale':
$options .= ' --' . $name . '=' . escapeshellarg($value);
break;
case 'proportional':
case 'textual':
$options .= ' --' . $name;
break;
default:
$error = 'Unknown option: ' . $name;
break 2;
}
}
if ( $error ) {
return '<strong class="error">'.htmlspecialchars($error).'</strong>';
}

$extension = $format;

$hash = md5( 'aafigure' . $options . "\n" . $input );
$filename = $dir . $hash . '.' . $extension;

$cmd = $wgAafigureSettings->aafigure . $options . ' -o ' . $filename;

if ( ! ( file_exists( $filename ) ) )
{
if ( ! is_dir( $dir ) ) {
mkdir( $dir, 0777 );
}

$descriptors = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("pipe", "w")
);
$pipes = array();
$process = proc_open( $cmd, $descriptors, $pipes );
$retval = -1;

if ( is_resource( $process ) ) {
fwrite( $pipes[0], $input );
fclose( $pipes[0] );
$stdout = stream_get_contents( $pipes[1] );
$stderr = stream_get_contents( $pipes[2] );
fclose( $pipes[1] );
fclose( $pipes[2] );
$retval = proc_close( $process );
}

if ( $retval < 0 ) {
return '<strong class="error">failed to execute'
. $wgAafigureSettings->aafigure . '</strong>';
}
else if ( $retval > 0 ) {
return '<strong><pre class="error">aafigure failed with error code '
. $retval . ":\n" . htmlspecialchars($stderr) . '</pre></strong>';
}
}

$src = $wgUploadPath . $subdir . $hash . '.' . $extension;
if ( $format == 'svg' ) {
if ($width > 0)
$width = ' width="' . $width . '"';
if ($height > 0)
$height = ' height="' . $height . '"';
return '<object data="' . $src . $width . $height . '"></object>';
}
else {
return '<img src="' . $src . '" />';
}
}

?>
94 changes: 94 additions & 0 deletions README
@@ -0,0 +1,94 @@
This extension adds an <nowiki><aafigure></nowiki> tag to MediaWiki for
embedding an ASCII art image to be processed by the
[http://packages.python.org/aafigure/ aafigure] program.

== Usage ==

Enclose your ASCII art image in an <nowiki><aafigure></nowiki> tag. You may
use any of the following attributes in the tag to send to the 'aafigure'
program.

; format=''format''
: Image output format, if allowed by the administrator. E.g., "png", "jpg", "svg"

; scale=''float''
: Scaling factor for image size.

; aspect=''float''
: Aspect ratio for non-text.

; proportional
: Use a proportional font instead of a fixed-width font.

; textual
: Disable fill horizontal detection. This means that fills must be at least two rows high.

; foreground=''hexcolor''
: Foreground color as six hex digits, RRGGBB.

; background=''hexcolor''
: Background color as six hex digits, RRGGBB.

; fill=''hexcolor''
: Fill color as six hex digits, RRGGBB.

The following options only work when the output format is SVG. The width and
height arguments are recommended because the software does not properly figure
out how large the resulting image is.

; linewidth=''float''
: Line width.

; width=''pixels''
: Width of the SVG container in pixels.

; height=''pixels''
: Height of the SVG container in pixels.

== Example ==

<aafigure textual scale=0.9 background=ffccff>
/-----------+
| MediaWiki +----+
\-----------+ |
v
o-----> Awesome!
^
/-----------+ |
| aafigure +----+
\-----------+
</aafigure>

== Installation ==

Copy this package to an "Aafigure" sub-directory of your extensions directory
and add the following to your LocalSettings.php:

require_once("extensions/Aafigure/Aafigure.php");

Also, copy the content of this README to a [[Help:Aafigure]] page.

== Configuration ==

The following variables can be set in you LocalSettings.php file.

; $wgAafigureSettings->aafigure
: Path to aafigure executable. Default: "aafigure"

; $wgAafigureSettings->format
: Image file format, passed as --type to aafigure. Default: "png"

; $wgAafigureSettings->allowed_formats
: Array of image formats that users are allowed to use via the "format" option. Default: none

== Bugs ==

SVG support is lacking in many ways. First, it does not compute the width and
height automatically; if they are not given manually, the browser draws an
incorrectly sized container. Second, it should use img tags on webkit rather
than an object tag. Finally, it should fall back to PNG for non-SVG browsers
(namely Internet Explorer).

== License ==

This work is licensed under the Expat (aka "MIT") license.

0 comments on commit b2e2169

Please sign in to comment.