You are here

function _field_hidden_readme2html in Field Hidden 7

Parse README.txt to html.

Replacements:

  • formats headlines; line with no hyphens followed by all hyphens line
  • hyphen to bullet; newline+space+*+space becomes newline+space+bullet+space
  • emphasizes underscore fragments; _what ever_ becomes <em>what ever</em>
  • turns url into link, and http://your-drupal-site.tld/some-path becomes internal link to /some-path
  • turns mail address into mailto link

Some replacements (like emphasizing) only works for ascii letters, not for letters line like ñ or ö.

Parameters

string $readme_txt:

string $headline_tag:

  • default: h5

Return value

string

File

./field_hidden.admin.inc, line 123
Drupal Field Hidden module administration

Code

function _field_hidden_readme2html($readme_txt, $headline_tag = 'h5') {
  if (!strlen($readme_txt)) {
    return 'empty readme';
  }
  $ndls = array(
    '/</',
    '/>/',
    '/\\r\\n|\\r/',
    // CR -> NL
    '/[\\x20\\t]+\\n/',
    // trailing spaces (in lines)
    '/\\n{3,}/',
    // 3 or more newlines to double
    '/<([^>@]+@[^>@]+)>/',
    // mail addresses
    '/([\\n\\x20])_([a-zA-Z\\d][^\\.,;\\:\\n\\x20]*[a-zA-Z\\d])_([\\n\\x20\\.,;\\:])/',
  );
  $rplcs = array(
    '&lt;',
    '&gt;',
    "\n",
    "\n",
    "\n\n",
    '<a href="mailto:$1">&#60;$1&#62;</a>',
    '$1<em>$2</em>$3',
  );
  $s = preg_replace($ndls, $rplcs, $readme_txt);

  //  Insert links in CONTENTS OF THIS FILE.
  if (strpos($s, 'CONTENTS OF THIS FILE') !== FALSE) {
    $pos_start = strpos($s, ' * ');
    $pos_end = strpos($s, "\n\n", $pos_start);
    $s_toc = preg_replace('/[ ]+\\n/', "\n", substr($s, $pos_start, $pos_end - $pos_start + 1));

    // remove trailing space(s)

    //echo $s_toc; exit;
    $s_toc = preg_replace_callback('/ \\* ([^\\n]+)\\n/', function ($ms) {
      return ' * <a href="#' . strtolower(preg_replace('/[^a-zA-Z_\\-]/', '_', $ms[1])) . '">' . $ms[1] . '</a>' . "\n";
    }, $s_toc);
    $s_start = substr($s, 0, $pos_start);
    $s_end = substr($s, $pos_end);
    $s = str_replace('CONTENTS OF THIS FILE', 'CONTENTS', $s_start) . $s_toc . $s_end;
  }

  //  Format headlines, and insert anchor.
  $s = preg_replace_callback('/\\n([^\\n\\-])([^\\n]*[^\\n\\-])\\n[\\-]{2,}\\n+/', function ($ms) {
    return '</p><a name="' . strtolower(preg_replace('/[^a-zA-Z_\\-]/', '_', $ms[1] . $ms[2])) . '"></a><headlineTag style="margin-top:20px;">' . $ms[1] . '<span style="text-transform:lowercase;">' . $ms[2] . '</span></headlineTag><p>';
  }, $s);
  $s = str_replace('headlineTag', $headline_tag, $s);
  $ndls = array(
    '/^[^\\n]*\\n/',
  );
  $rplcs = array(
    '',
  );
  $s = preg_replace($ndls, $rplcs, $s);

  //  Links may be followed by a punctuation marker, hard to detect without a callback.
  //  And http://your-drupal-site.tld/path should become /path
  $s = preg_replace_callback('/(https?\\:\\/\\/)(\\S+)([\\x20\\t\\n])/', function ($ms) {
    $protocol = $ms[1];
    $le = strlen($addr = $ms[2]);
    $dot = '';
    if (!preg_match('/[\\/a-zA-Z\\d]/', $addr[$le - 1])) {
      $dot = $addr[$le - 1];
      $addr = substr($addr, 0, $le - 1);
    }
    if (strpos($addr, 'your-drupal-site.tld/') === 0) {
      $protocol = '/';
      $addr = str_replace('your-drupal-site.tld/', '', $addr);
    }
    return '<a href="' . $protocol . $addr . '">' . $addr . '</a>' . $dot . $ms[3];
  }, $s);
  $ndls = array(
    '/([\\n>]) \\* /',
    // \n *  to \n bullet
    '/^\\n?<\\/p>/',
    // first ending <p> added by headline (in total)
    '/\\n?$/',
    // trailing newlines (in total)
    '/\\n{2,}/',
  );
  $rplcs = array(
    '$1 &bull; ',
    '',
    '',
    '</p><p>',
  );
  return nl2br(preg_replace($ndls, $rplcs, $s)) . '</p>';
}