--- /Users/bwynants/Desktop/inline/inline.module 2005-05-19 04:08:43.000000000 +0200
+++ /Users/bwynants/Sites/blog/modules/inline/inline.module 2005-10-04 11:04:40.000000000 +0200
@@ -1,8 +1,7 @@
using special tags', array('%explanation-url' => url('filter/tips', NULL, 'image')));
case 'filter#long-tip':
- return t('
You may link to files uploaded with the current node using special tags. The tags will be replaced by the corresponding files. Syntax: [inline:file_id]. Parameter: file_id represents the file uploaded with the node in which to link, assuming that the first uploaded file is labeled as 1 and so on.
+ return t('You may link to files uploaded with the current node using special tags. The tags will be replaced by the corresponding files. Syntax: [attachment:file_id]. Parameter: file_id represents the file uploaded with the node in which to link, assuming that the first uploaded file is labeled as 1 and so on.
If the file is an image, it will be displayed inline, otherwise a link to the file will be inserted.
');
}
}
-function inline_settings() {
- return form_textfield(t('Maximum width and height for the displayed inline images (format: XXX,YYY)'), 'inline_img_dim', variable_get('inline_img_dim', '150,150'), 10, 10, t('This setting will affect the dimensions of displayed images. They will not be resized.'));
-}
function inline_filter($op, $delta = 0, $format = -1, $text = '') {
// The "list" operation provides the module an opportunity to declare both how
@@ -35,7 +31,7 @@ function inline_filter($op, $delta = 0,
// discuss each filter in turn.
switch ($op) {
case 'description':
- return t('Substitutes [inline:xx] tags with the xxth file uploaded with the node.');
+ return t('Substitutes [attachment:xx] tags with the xxth file uploaded with the node.');
case 'prepare':
return $text;
case 'process':
@@ -45,60 +41,97 @@ function inline_filter($op, $delta = 0,
function inline_filter_tips($delta, $format, $long = false) {
if ($long) {
- return t('
- You may link to files uploaded with the current node using special tags. The tags will be replaced by the corresponding files. For example:
-
- Suppose you uploaded three files (in this order):
-
- - imag1.png (referred as file #1)
-
- file1.pdf (referred as file #2)
-
- imag2.png (referred as file #3)
-
-
- [inline:1=test] or [inline:imag1.png=test]
-
- will be replaced by <img src=imag1.png alt=test>
-
-
- [file:1=test] or [file:imag1.png=test]
-
- will be replaced by <a href=imag1.png>test</a>
-
-
- [attachment:2=test] or [attachment:file1.pdf=test]
-
- will be replaced by <a href=file1.pdf.png>test</a>
-
- ');
+ return t('You may quickly link to attachments using a special syntax. Each attachment code will be replaced by an image or link to the attachment. Syntax:
+ [attachment:attachment_ref link align=alignment hspace=n vspace=n border=n width=n height=n title=name class=name style=style-data]
+ Every parameter except attachment_ref is optional.
+ attachment_ref can be the filename or the index of the attachment. When using filename be sure to put it between quotes (") when the name contains spaces .
+ Typically, you will specify one of width, or height, or none of them. If you use width=n or height=n, the image will be scaled to fit the specified width or height. If you use none of them, the attached image size will be used.
+ The align, hspace, vspace, border, class, and style parameters set the corresponding attributes in the generated img tag.
+ By default an attached image is displayed as an image not as a link. However if you specify link a link to the image is created.
');
}
else {
- return t('You may use [inline:xx] tags to display uploaded files or images inline.', array("%inline_help" => url("filter/tips/$format", NULL, 'filter-inline')));
+ return t(
+ 'You may link to attachments using a special syntax', array("%inline_help" => url("filter/tips/$format", NULL, 'filter-inline')));
+ }
+}
+
+define("INLINE_WORD", 1);
+define("INLINE_INTEGER", 2);
+define("INLINE_STRING", 3);
+
+function inline_attr_value($text, $value_type = INLINE_WORD) {
+ // Strip off initial and final quotes.
+ $first = substr($text, 0, 1);
+ if ($first == "\"" || $first == "\'") {
+ if (substr($text, -1, 1) == $first) {
+ $text = substr($text, 1, -1);
+ }
+ }
+ switch ($value_type) {
+ case INLINE_WORD:
+ return preg_replace("/\W/", '', $text);
+ case INLINE_INTEGER:
+ return preg_replace("/\D/", '', $text);
+ default:
+ return check_plain($text);
}
}
function inline_nodeapi(&$node, $op, $arg) {
if(is_array($node->files) && $op == 'view') {
- if (preg_match_all("/\[(inline|file|attachment):([^=\\]]+)=?([^\\]]*)?\]/i", $node->body, $match)) {
- foreach ($match[2] as $key => $value) {
- $map[$value] = $key;
- $titl = $match[3][$key];
- $mytype = $match[1][$key];
- $inline_file = _inline_filename($node, $value);
- $replace = "";
- if ($inline_file != NULL) {
- $replace = theme('inline_html', $inline_file[0], $inline_file[1], $titl, $mytype == "inline");
- }
- else {
- $replace = "NOT FOUND: $value";
- }
- $mtch[] = $match[0][$key];
- $repl[] = $replace;
- }
- $text_b = str_replace($mtch, $repl, $node->body);
- $node->body = $text_b;
- $text_t = str_replace($mtch, $repl, $node->teaser);
- $node->teaser = $text_t;
- }
+ //
+ // check for \= in [^\s\]\=] is only for compatibility reasons with previous module -> causes filenames including an = to fail!
+ //
+ if (preg_match_all("/\[(inline|file|attachment):(\"[^\"]*\"|[^\s\]\=]*)(\s*,)?\s*(.*?)\]/i", $node->body, $matches, PREG_SET_ORDER)) {
+ foreach ($matches as $match) {
+ $args = array();
+ // Convert bare alignment 'X' to 'align="X"'.
+ $match[4] = preg_replace("/^(left|right|top|middle|bottom|absmiddle|texttop|baseline)\b/i", "align=\"$1\"", $match[4]);
+
+ // any keywords ?
+ preg_match_all("/(\w+)\=(\"[^\"]*\"|\S*)|(\w+)\s/", $match[4], $a, PREG_SET_ORDER);
+ foreach ($a as $arg) {
+ if (isset($arg[3])) {
+ // keyword only
+ $args[strtolower($arg[3])] = 1;
+ }
+ else {
+ // keyword and value
+ $args[strtolower($arg[1])] = $arg[2];
+ }
+ }
+
+ $param->width = inline_attr_value($args['width'], INLINE_INTEGER);
+ $param->height = inline_attr_value($args['height'], INLINE_INTEGER);
+ $param->align = inline_attr_value($args['align'], INLINE_WORD);
+ $param->hspace = inline_attr_value($args['hspace'], INLINE_INTEGER);
+ $param->vspace = inline_attr_value($args['vspace'], INLINE_INTEGER);
+ $param->border = inline_attr_value($args['border'], INLINE_INTEGER);
+ $param->class = inline_attr_value($args['class'], INLINE_WORD);
+ $param->style = inline_attr_value($args['style'], INLINE_STRING);
+ $param->title = inline_attr_value($args['title'], INLINE_STRING);
+ if (isset($args['link'])) {
+ $param->inline = false;
+ }
+ else {
+ $param->inline = true;
+ }
+ $inline_file = _inline_filename($node, inline_attr_value($match[2], INLINE_STRING));
+ $replace = "";
+ if ($inline_file != NULL) {
+ $replace = theme('inline_html', $inline_file[0], $inline_file[1], $param);
+ }
+ else {
+ $replace = "NOT FOUND: $value";
+ }
+ $mtch[] = $match[0];
+ $repl[] = $replace;
+ }
+ $text_b = str_replace($mtch, $repl, $node->body);
+ $node->body = $text_b;
+ $text_t = str_replace($mtch, $repl, $node->teaser);
+ $node->teaser = $text_t;
+ }
}
}
@@ -107,7 +140,6 @@ function _inline_filename(&$node, $id) {
$n=1;
foreach ($node->files as $file) {
if ($n == $id) {
- //return array($file->filename, file_create_url($file->filepath));
return array($file->filename, $file->filepath);
}
++$n;
@@ -118,45 +150,52 @@ function _inline_filename(&$node, $id) {
{
foreach ($node->files as $file) {
if ($file->filename == $id) {
- return array($file->filename, file_create_url($file->filepath));
+ return array($file->filename, $file->filepath);
}
}
return NULL;
}
}
-function theme_inline_html($filename, $filepath, $title, $allow_inline_image) {
+function theme_inline_html($filename, $filepath, $param) {
//make a list with allowed image-tags
+ global $base_url;
$extensions = 'jpg jpeg gif png';
$regex = '/\.('. ereg_replace(' +', '|', preg_quote($extensions)) .')$/i';
- if (preg_match($regex, $filepath) && $allow_inline_image) {
- $f_dim=getimagesize($filepath);
- $f_width=$f_dim[0];
- $f_height=$f_dim[1];
- $ratio = 0; if ($f_height) $ratio=($f_width*100)/$f_height;
- // read settings
- $dim=explode(',',variable_get('inline_img_dim', '150,150'));
- $width=$dim[0];
- $height=$dim[0];
- // maintain aspect ration
- if ($f_width > $width) {
- $f_width=$width;
- $f_height=intval($f_width*100/$ratio);
+ if (preg_match($regex, $filepath) && $param->inline) {
+ $f_dim = getimagesize($filepath);
+
+ if (!$param->width && !$param->height) {
+ $param->width = $f_dim[0];
+ $param->height = $f_dim[1];
}
- if ($title != "") {
- $html = '
';
+ else if ($param->width && ! $param->height) {
+ $param->height = round($f_dim[1] * $width / $f_dim[0]);
}
- else {
- $html = '
';
+ else if ($param->height && ! $param->width) {
+ $param->width = round($f_dim[0] * $height / $f_dim[1]);
}
+
+ $html = "
width ? " width=\"$param->width\"" : '') .
+ ($param->height ? " height=\"$param->height\"" : '') .
+ ($param->align ? " align=\"$param->align\"" : '') .
+ ($param->border ? " border=\"$param->border\"" : '') .
+ ($param->hspace ? " hspace=\"$param->hspace\"" : '') .
+ ($param->vspace ? " vspace=\"$param->vspace\"" : '') .
+ ($param->title ? " title=\"$param->title\" alt=\"$param->title\"" : " alt=\"$filename\"") .
+ ($param->class ? " class=\"$param->class\"" : " class=\"inline\"") .
+ ($param->style ? " style=\"$param->style\"" : '') . " />";
}
else {
- if ($title != "") {
- $html = ''. $title .'';
- }
- else {
- $html = ''. $filename .'';
- }
+ $html = "align ? " align=\"$param->align\"" : '') .
+ ($param->title ? " title=\"Download: $param->title\"" : " title=\"Download: $filename\"") .
+ ($param->class ? " class=\"$param->class\"" : " class=\"inline\"") .
+ ($param->style ? " style=\"$param->style\"" : '') .
+ "\">".
+ ($param->title ? "$param->title" : "$filename") .
+ "";
}
return $html;
}