You are here

private function HamlParser::parseAttributes in Sassy 7

* Parse attributes. *

Parameters

array line to parse: * @return array attributes in name=>value pairs

1 call to HamlParser::parseAttributes()
HamlParser::parseElement in phamlp/haml/HamlParser.php
* Parse an element. *

File

phamlp/haml/HamlParser.php, line 856

Class

HamlParser
HamlParser class. Parses {@link http://haml-lang.com/ Haml} view files. @package PHamlP @subpackage Haml

Code

private function parseAttributes($line) {
  $attributes = array();
  if (!empty($line[self::HAML_OPEN_XML_ATTRIBUTES])) {
    if (empty($line[self::HAML_XML_ATTRIBUTES])) {
      $line[self::HAML_XML_ATTRIBUTES] = $line[self::HAML_CONTENT];
      unset($line[self::HAML_CONTENT]);
      do {
        $multiLine = array_shift($this->source);
        $line[self::HAML_XML_ATTRIBUTES] .= $multiLine[self::HAML_CONTENT];
      } while (substr($line[self::HAML_XML_ATTRIBUTES], -1) !== self::CLOSE_XML_ATTRIBUTES);
    }
    if (preg_match(self::HTML_ATTRS, $line[self::HAML_XML_ATTRIBUTES], $htmlAttrs)) {
      $line[self::HAML_XML_ATTRIBUTES] = preg_replace(self::HTML_ATTRS, '', $line[self::HAML_XML_ATTRIBUTES]);
      $attributes = array_merge($attributes, $this
        ->htmlAttrs($htmlAttrs));
    }
    $attributes = array_merge($attributes, $this
      ->parseAttributeHash($line[self::HAML_XML_ATTRIBUTES]));
  }
  if (!empty($line[self::HAML_OPEN_RUBY_ATTRIBUTES])) {
    if (empty($line[self::HAML_RUBY_ATTRIBUTES])) {
      $line[self::HAML_RUBY_ATTRIBUTES] = $line[self::HAML_CONTENT];
      unset($line[self::HAML_CONTENT]);
      do {
        $multiLine = array_shift($this->source);
        $line[self::HAML_RUBY_ATTRIBUTES] .= $multiLine[self::HAML_CONTENT];
      } while (substr($line[self::HAML_RUBY_ATTRIBUTES], -1) !== self::CLOSE_RUBY_ATTRIBUTES);
    }
    if (preg_match(self::HTML_ATTRS, $line[self::HAML_RUBY_ATTRIBUTES], $htmlAttrs)) {
      $line[self::HAML_RUBY_ATTRIBUTES] = preg_replace(self::HTML_ATTRS, '', $line[self::HAML_RUBY_ATTRIBUTES]);
      $attributes = array_merge($attributes, $this
        ->htmlAttrs($htmlAttrs));
    }
    $attributes = array_merge($attributes, $this
      ->parseAttributeHash($line[self::HAML_RUBY_ATTRIBUTES]));
  }
  if (!empty($line[self::HAML_OBJECT_REFERENCE])) {
    $objectRef = explode(',', preg_replace('/,\\s*/', ',', $line[self::HAML_OBJECT_REFERENCE]));
    $prefix = isset($objectRef[1]) ? $objectRef[1] . '_' : '';
    $class = "strtolower(str_replace(' ',\t'_', preg_replace('/(?<=\\w)([ A-Z])/', '_\1', get_class(" . $objectRef[0] . '))))';
    $attributes['class'] = "<?php echo '{$prefix}' . {$class}; ?>";
    $attributes['id'] = "<?php echo '{$prefix}' . {$class} . '_' . {$objectRef[0]}->id; ?>";
  }
  else {
    if (!empty($line[self::HAML_CLASS])) {
      $classes = explode('.', $line[self::HAML_CLASS]);
      foreach ($classes as &$class) {
        if (preg_match(self::MATCH_INTERPOLATION, $class)) {
          $class = $this
            ->interpolate($class);
        }
      }

      // foreach
      $attributes['class'] = join(' ', $classes) . (isset($attributes['class']) ? " {$attributes['class']}" : '');
    }
    if (!empty($line[self::HAML_ID])) {
      $attributes['id'] = (preg_match(self::MATCH_INTERPOLATION, $line[self::HAML_ID]) ? $this
        ->interpolate($line[self::HAML_ID]) : $line[self::HAML_ID]) . (isset($attributes['id']) ? "_{$attributes['id']}" : '');
    }
  }
  ksort($attributes, SORT_STRING);
  return $attributes;
}