mihdan
7/6/2018 - 8:46 PM

PHP Code to convert an emmet-like string into HTML markup, with a string of content within it.

PHP Code to convert an emmet-like string into HTML markup, with a string of content within it.

<?php

/**
 * Reads and parses a csv file into an indexed or associative array.
 *
 * @param string $csv_filepath
 *   The path of the csv file to read and parse.
 * @param string $unique_index_by
 *   The name of a column present in the CSV, to return results indexed by the
 *   values of that column. (It assumes the $unique_index_by value will be
 *   unique in the CSV for the column specified).
 * @return array|null
 *   The indexed or associative array, if the parsing was successful. NULL
 *   otherwise.
 */
function read_and_parse_csv($csv_filepath, $unique_index_by = NULL) {
  $results = NULL;
  // Read csv file.
  if (file_exists($csv_filepath)) {
    if ($handle = fopen($csv_filepath, 'r')) {
      $csv_headers = fgetcsv($handle);

      // Populate an array with the csv data. Index data by the $index_by value,
      // if specified
      $results = array();
      while ($csv_row = fgetcsv($handle)) {
        $result_row = array();
        foreach ($csv_row as $index => $value) {
          // Add each column value, keyed by the column name.
          $result_row[$csv_headers[$index]] = $value;
        }

        // Add the row to the results array. If specified, index it using the
        // value of the field (column) specified.
        if (!is_null($unique_index_by)) {
          $results[$result_row[$unique_index_by]] = $result_row;
        }
        else {
          $results[] = $result_row;
        }
      }
    }
    fclose($handle);
  }
  return $results;
}
<?php

/**
 * @file
 * emmet_wrapper module.
 * Helper classes to wrap contents with markup generated from emmet strings.
 */

/**
 * Expands an emmet-like string into actual HTML markup.
 *
 * @param string $emmet_string
 *  The emmet-like string.
 * @param string $content
 *  Content to insert in the middle of the expanded markup.
 * @return string
 *  The expanded markup, with the content in the middle of it.
 */
function wrapper_expand($emmet_string, $content = '') {
  // Split string elements. Allow 1-level grouping.
  $wrapper_elements = preg_split('/>(?!(\w+)\))/', $emmet_string, NULL, PREG_SPLIT_NO_EMPTY);

  // Wrap contents.
  $output = wrapper_process_elements($wrapper_elements, $content);
  return $output;
}

/**
 * Process an array containing emmet-like string parts to generate markup.
 *
 * @param array $elements_array
 *  An array the components of an emmet-like string.
 * @param string $content
 *  Content to insert in the middle of the expanded markup.
 * @return string
 *  The expanded markup, with the content in the middle of it.
 */
function wrapper_process_elements($elements_array, $content) {
  $output = '';

  $current_element = array_shift($elements_array);
  $processed_element = process_emmet_element($current_element);
  $output .= $processed_element['prefix'];
  if (empty($elements_array)) {
    $output .= $content;
  }
  else {
    $output .= wrapper_process_elements($elements_array, $content);
  }
  $output .= $processed_element['suffix'];
  return $output;
}

/**
 * Generates the markup for a single emmet string component.
 *
 * @param string $element
 *  The element string.
 * @return string
 *  The markup for the element passed.
 */
function process_emmet_element($element) {
  $processed_element = array();
  preg_match('/\w*/', $element, $matches);
  $element_tag = reset($matches);

  // Check for ids. (Makes no sense to accept multiple, but it's still supported).
  preg_match_all('/\#[a-z-A-Z0-9]*/', $element, $ids);
  $ids = reset($ids);
  array_walk($ids, '_remove_prefix_symbol');

  // Check for classes.
  preg_match_all('/\.[a-z-A-Z0-9\-\_]*/', $element, $res);
  $classes = reset($res);
  array_walk($classes, '_remove_prefix_symbol');

  // Check for attributes.
  preg_match_all('/\[([^\w-\]]|[a-zA-Z0-9])*\]/', $element, $attr_strings);
  $attr_strings = reset($attr_strings);
  array_walk($attr_strings, '_remove_prefix_symbol');
  array_walk($attr_strings, '_remove_suffix_symbol');

  $processed_element['prefix'] = '<' . $element_tag;
  if ($ids) {
    $processed_element['prefix'] .= ' id="' . implode(' ', $ids) . '"';
  }

  if ($classes) {
    $processed_element['prefix'] .= ' class="' . implode(' ', $classes) . '"';
  }
  if ($attr_strings) {
    $processed_element['prefix'] .= ' ' . implode(' ', $attr_strings);
  }

  $processed_element['prefix'] .= '>';
  $processed_element['suffix'] = '</' . $element_tag . '>';

  return $processed_element;
}

/**
 * Callback to remove the attribute prefix symbol from an emmet attribute string.
 *
 * @see process_emmet_element();
 */
function _remove_prefix_symbol(&$value, $key) {
  $value = substr($value, 1);
}

/**
 * Callback to remove the attribute suffix symbol from an emmet attribute string.
 *
 * @see process_emmet_element();
 */
function _remove_suffix_symbol(&$value, $key) {
  $value = substr($value, 0, strlen($value) - 1);
}