1

Topic: WYSIWYG 'span' to 'em', etc.

Hey i just started using htmLawed and i am going to create a plugin with it for symfony. But i have some questions. I want to use it to clean up wysiwyg editor html and then have the html converted into markdown.

But this seems to be kinda hard, or i am missing some configuration. My wysiwyg editor produces <span style="font-weight: bold">bold text</span> which is correctly XHTML but inline styles suck and the markdown library can only convert <strong> <b> tags.

Is there a configuration or something that will convert stupid <span> tags into <strong> ? and the same for <em> <u> etc.

Henrik :)
yep [at] iamhenrik [dot] se

2

Re: WYSIWYG 'span' to 'em', etc.

This'd probably require using the 'hook_tag' parameter. The value would be the name of a customized function that you'll have to write. The function is passed every opening tag element and any associated attribute; see the documentation for more.

Example, partial code:

// Assign the function name to hook_tag
$config = array(... 'hook_tag' => 'my_tag_function'...);

// Main htmLawed code
$out = htmLawed($in, $config...);

// The customized function
function my_tag_function($element, $attribute_array){
  // Care only if 'span' with 'style'
  if($element == 'span' && isset($attribute_array['style'])){
    if($attribute_array['style'] == 'font-weight=bold;'){
      $element = 'strong';
      $attribute_array = array();
    }
    ...
  }

  $string = '';
  if(!empty($attribute_array)){
    foreach($attribute_array as $k=>$v){
      $string .= " {$k}=\"{$v}\"";
    }
  }

  // Empty HTML elements
  static $empty_elements = array('br', 'img'...);

  // Full opening tag content is to be returned
  // '<span style=...>' will become '<strong>', etc.

  return "<{$element}{$string}". (isset($in_array($element, $empty_elements) ? ' /' : ''). '>';
} 

During tag balancing, the closing '</span>' will be removed but I am not sure where '</strong>', etc., will end up. Let me know. May be this would require tweaking htmLawed a bit.

3

Re: WYSIWYG 'span' to 'em', etc.

As of the new version, 1.1.11, of htmLawed, the 'hook_tag' function also receives contents of closing tags. See documentation.