--- inline.cvs	2006-05-04 03:03:00.000000000 +0200
+++ inline.module.filtercode	2006-05-22 22:59:22.811988200 +0200
@@ -29,30 +29,25 @@ function inline_settings() {
     '#description' => t('This setting will affect the dimensions of displayed images. They will not be resized. Images larger than these dimensions will not be shown automatically.'),
   );
 
+  $form['inline_img_space_dim'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Default image spacing (horizontal, vertical)'),
+    '#default_value' => variable_get('inline_img_space_dim', '5,5'),
+    '#size' => 10,
+    '#maxlength' => 10,
+    '#description' => t('This setting will affect the white space around the images. Use <em>hspace=n</em> or <em>vspace=n</em> to override default for specific images.'),
+  );
   $form['inline_link_img'] = array(
     '#type' => 'radios',
     '#title' => t('Print link to images'),
     '#default_value' => variable_get('inline_link_img', 1),
     '#options' => array('0' => t('Print only &lt;img&gt; tag'),'1' => t('Print &lt;img&gt; tag and the link to the image')),
+    '#default_value' => $edit['custom'],
   );
 
   return $form;
 }
 
-function inline_form_alter($form_id, &$form) {
-  if (isset($form['type'])) {
-    if ($form['type']['#value'] .'_node_settings' == $form_id) {
-      $form['workflow']['upload_inline_'. $form['type']['#value']] = array(
-          '#type' => 'radios',
-          '#title' => t('Display attachments inline automatically'),
-          '#default_value' => variable_get('upload_inline_'. $form['type']['#value'], 0),
-          '#options' => array(t('Disabled'), t('Only in teaser'), t('Only in body'), t('In teaser and body')),
-          '#description' => t('Whether or not uploaded images should be shown inline. Make sure you set the dimensions at %settings_url', array('%settings_url' => l(t('inline settings'), 'admin/settings/inline'))),
-      );
-    }
-  }
-}
-
 function inline_filter($op, $delta = 0, $format = -1, $text = '') {
   // The "list" operation provides the module an opportunity to declare both how
   // many filters it defines and a human-readable name for each filter. Note that
@@ -67,7 +62,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':
@@ -76,35 +71,22 @@ function inline_filter($op, $delta = 0, 
 }
 
 function inline_filter_tips($delta, $format, $long = false) {
-  if ($long) {
-    return t('
-    <p>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):
-    <ul>
-    <li>imag1.png (referred as file #1)
-    <li>file1.pdf (referred as file #2)
-    <li>imag2.png (referred as file #3)
-    </ul>
-
-    <pre>[inline:1=test]  or  [inline:imag1.png=test]</pre>
-
-    will be replaced by <em><code>&lt;img src=imag1.png alt=test&gt;</code></em>
-
-
-    <pre>[file:1=test]  or  [file:imag1.png=test]</pre>
-
-    will be replaced by <em><code>&lt;a href=imag1.png&gt;test&lt;/a&gt;</code></em>
-
-
-    <pre>[attachment:2=test]  or  [attachment:file1.pdf=test]</pre>
-
-    will be replaced by <em><code>&lt;a href=file1.pdf.png&gt;test&lt;/a&gt;</code></em>
-
-    ');
+  if (user_access('upload files')) {
+    if ($long) {
+	  return t('<p>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:</p>
+		  	    <blockquote><code>[attachment:<em>attachment_ref</em> link align=<em>alignment</em> hspace=<em>n</em> vspace=<em>n</em> border=<em>n</em> width=<em>n</em> height=<em>n</em> title=<em>name</em> class=<em>name</em> style=<em>style-data</em>]</code></blockquote>
+				<p>Every parameter except <em>attachment_ref</em> is optional.</p>
+				<p><em>attachment_ref</em> 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 .</p>
+				<p>Typically, you will specify one of <code>width</code>, or <code>height</code>, or none of them. If you use <code>width=<em>n</em></code> or <code>height=<em>n</em></code>, 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.</p>
+				<p>The <code>align</code>, <code>hspace</code>, <code>vspace</code>, <code>border</code>, <code>class</code>, and <code>style</code> parameters set the corresponding attributes in the generated <code>img</code> tag.</p>
+				<p>By default an attached image is displayed as an image not as a link. However if you specify <code>link</code> a link to the image is created.</p>');
+    }
+    else {
+      return t('You may use <a href="%inline_help">[inline:xx] tags</a> to display uploaded files or images inline.', array("%inline_help" => url("filter/tips/$format", NULL, 'filter-inline')));
+    }
   }
   else {
-    return t('You may use <a href="%inline_help">[inline:xx] tags</a> to display uploaded files or images inline.', array("%inline_help" => url("filter/tips/$format", NULL, 'filter-inline')));
+	return "";
   }
 }
 
@@ -113,18 +95,37 @@ function inline_nodeapi(&$node, $op, $ar
     //TODO: we should *really* us the filter hooks for this, not?
     $node->teaser = _inline_substitute_tags($node, 'teaser');
     $node->body = _inline_substitute_tags($node, 'body');
-    if (variable_get('upload_inline_'. $node->type, 0)) {
-      $node = _inline_auto_add($node);
+  }
+}
+
+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_fileobj(&$node, $id) {
+function _inline_filename(&$node, $id) {
   if (is_numeric($id)) {
     $n=1;
     foreach ($node->files as $file) {
       if ($n == $id) {
-        return $file;
+        return array($file->filename, $file->filepath);
       }
       ++$n;
     }
@@ -134,133 +135,130 @@ function _inline_fileobj(&$node, $id) {
   {
     foreach ($node->files as $file) {
       if ($file->filename == $id) {
-        //return array($file->filename, $file->filepath);
-        return $file;
+        return array($file->filename, $file->filepath);
       }
     }
     return NULL;
   }
 }
 
-function theme_inline_as_link($file) {
-  return l(($file->description ? $file->description : $file->name),
-    file_create_url($file->filepath),
-    array('title' => t('Download: %name (%size)',
-      array('%name' => $file->filename, '%size' => format_size($file->filesize)))));
-}
-
-function theme_inline_img($file) {
-  if (variable_get('inline_link_img', '1') == '1') {
-    $html .= '<a href="'. file_create_url($file->filepath) . '" title="'.t("View").': '. $file->name .'">';
+function theme_inline_html($filename, $filepath, $param) {
+  //make a list with allowed image-tags
+  $extensions = 'jpg jpeg gif png';
+  $regex = '/\.('. ereg_replace(' +', '|', preg_quote($extensions)) .')$/i';
+  if (preg_match($regex, $filepath) && $param->inline) {
+    $f_dim = getimagesize($filepath);
+
+    // Adjust to supplied sizes
+    if (!$param->width && !$param->height) {
+	  $param->width = $f_dim[0];
+	  $param->height = $f_dim[1];
+    }
+    else if ($param->width && ! $param->height) {
+	  $param->height = round($f_dim[1] * $width / $f_dim[0]);
+    }
+    else if ($param->height && ! $param->width) {
+	  $param->width = round($f_dim[0] * $height / $f_dim[1]);
+    }
+    
+    // Adjust to max sizes
+    $maxdim=explode(',',variable_get('inline_img_dim', '150,150'));
+    if ($param->width > $maxdim[0]) {
+      $param->width = $maxdim[0];
+	  $param->height = round($f_dim[1] * $param->width / $f_dim[0]);
+    }
+    
+    if ($param->height > $maxdim[1]) {
+      $param->height = $maxdim[1];
+	  $param->width = round($f_dim[0] * $param->height / $f_dim[1]);
+    }
+
+    $html = '';
+    if (variable_get('inline_link_img', '1') == '1') {
+      $html .= '<a href="'. file_create_url($filepath) . '" title="'.t("View").': '. $filename .'">';
+    }
+    
+    $dim=explode(',',variable_get('inline_img_space_dim', '5,5'));
+   
+    $html .= "<img src=\"".file_create_url($filepath)."\"".
+		($param->width ? " width=\"$param->width\"" : '') .
+		($param->height ? " height=\"$param->height\"" : '') .
+		($param->align ? " align=\"$param->align\"" : '') .
+		($param->border ? " border=\"$param->border\"" : '') .
+		($param->hspace ? " hspace=\"$param->hspace\"" : " hspace=\"$dim[0]\"") .
+		($param->vspace ? " vspace=\"$param->vspace\"" : " vspace=\"$dim[1]\"") .
+		($param->title ? " title=\"$param->title\" alt=\"$param->title\"" : " alt=\"$filename\"") .
+		($param->class ? " class=\"$param->class\"" : " class=\"inline\"") .
+		($param->style ? " style=\"$param->style\"" : '') . " />";
+	if (variable_get('inline_link_img', '1') == '1') {
+	  $html .= '</a>';
+	}
   }
-
-  $title = ( $file->description ? $file->description : $file->name );
-
-  $html .= '<img style="width: '.$file->width.'px; height:'.$file->height.'px;" src="'. file_create_url($file->filepath). '" class="inline" alt="'. $title .'" title="'. $title.'" />';
-
-  if (variable_get('inline_link_img', '1') == '1') {
-    $html .= '</a>';
+  else {
+    $html = "<a href=\"".file_create_url($filepath)."\"".
+		($param->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") .
+		"</a>";
   }
   return $html;
 }
 
-function theme_inline_add_to_teaser($node, $file) {
-   return theme('inline_img', $file) . $node->teaser;
-}
-
-function theme_inline_add_to_body($node, $file) {
-   return theme('inline_img', $file) . $node->body;
-}
-
-function _inline_auto_add($node) {
-  $inline_file = _inline_fileobj($node, $value);
-  //0 Disabled
-  //1 Only in teaser
-  //2 Only in body
-  //3 In teaser and body
-  switch (variable_get('upload_inline_'. $node->type, 0)) {
-    case 1:
-      foreach ($node->files as $fid => $file) {
-        if (_inline_decide_img_tag($file)) {
-          $node->files[$fid]->inline = TRUE;
-          $node->teaser = theme('inline_add_to_teaser', $node, $file);
-        }
-        else {
-          $node->files[$fid]->inline = FALSE;
-        }
-      }
-    break;
-    case 2:
-      foreach ($node->files as $fid => $file) {
-        if (_inline_decide_img_tag($file)) {
-          $node->files[$fid]->inline = TRUE;
-          $node->body = theme('inline_add_to_body', $node, $file);
-        }
-        else {
-          $node->files[$fid]->inline = FALSE;
-        }
-      }
-    break;
-    case 3:
-      foreach ($node->files as $fid => $file) {
-        if (_inline_decide_img_tag($file)) {
-          $node->files[$fid]->inline = TRUE;
-          $node->teaser = theme('inline_add_to_teaser', $node, $file);
-          $node->body = theme('inline_add_to_body', $node, $file);
-        }
-        else {
-          $node->files[$fid]->inline = FALSE;
-        }
-      }
-    break;
-  }
-  return $node;
-}
-
 function _inline_substitute_tags(&$node, $field) {
-  if (preg_match_all("/\[(inline|file|attachment):([^=\\]]+)=?([^\\]]*)?\]/i", $node->$field, $match)) {
-    foreach ($match[2] as $key => $value) {
-      $map[$value] = $key;
-      $titl = $match[3][$key];
-      $ytype = $match[1][$key];
-      $file = _inline_fileobj($node, $value);
-      $replace = "";
-      if ($file->fid != NULL) {
-        //decide if we should show a link or an img tag
-        if (_inline_decide_img_tag($file)) {
-          $replace = theme('inline_img', $file);
-        }
-        else {
-          $replace = theme('inline_as_link', $file);
-        }
-      }
-      else {
-        $replace = "<span style=\"color:red; font-weight:bold\">NOT FOUND: $value</span>";
-      }
-      $mtch[] = $match[0][$key];
-      $repl[] = $replace;
+  //
+  // 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->$field, $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 = "<span style=\"color:red; font-weight:bold\">NOT FOUND: $value</span>";
+	  }
+	  $mtch[] = $match[0];
+	  $repl[] = $replace;
     }
     return str_replace($mtch, $repl, $node->$field);
   }
   return $node->$field;
 }
 
-/**
- * Decides if a tag (&lt;img&gt;) or a link to a file should be rendered
- * @param $file a file object
- * @return TRUE in case an img tag should be generated
- */
-function _inline_decide_img_tag($file) {
-  $inlined = array('jpg', 'jpeg', 'pjpeg', 'gif', 'png');
-  $mime = array_pop(explode('/', $file->filemime));
-  if (in_array($mime, $inlined)) {
-    // read settings
-    list($maxwidth, $maxheight) = explode(',',variable_get('inline_img_dim', '150,150'));
-    list($width, $height) = getimagesize($file->filepath);
-    if (($width && $height) && ($width < $maxwidth && $height < $maxheight)) {
-      return TRUE;
-    }
-  }
-  return FALSE;
-}
 ?>
