OSDN Git Service

Separate generate_fixed_heading_anchor_id() from make_str_rules()
authorhenoheno <henoheno>
Tue, 5 Jul 2005 12:39:19 +0000 (21:39 +0900)
committerhenoheno <henoheno>
Tue, 5 Jul 2005 12:39:19 +0000 (21:39 +0900)
lib/file.php

index 9ad23f9..e07b254 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 // PukiWiki - Yet another WikiWikiWeb clone.
-// $Id: file.php,v 1.30 2005/07/03 14:16:23 henoheno Exp $
+// $Id: file.php,v 1.31 2005/07/05 12:39:19 henoheno Exp $
 // Copyright (C)
 //   2002-2005 PukiWiki Developers Team
 //   2001-2002 Originally written by yu-ji
@@ -101,12 +101,10 @@ function make_str_rules($source)
                
                // Adding fixed anchor into headings
                if ($fixed_heading_anchor &&
-                       preg_match('/^(\*{1,3}(.(?!\[#[A-Za-z][\w-]+\]))+)$/', $line, $matches))
-               {
-                       // Generate ID:
-                       // A random alphabetic letter + 7 letters of random strings from md()
-                       $anchor = chr(mt_rand(ord('a'), ord('z'))) .
-                               substr(md5(uniqid(substr($matches[1], 0, 100), 1)), mt_rand(0, 24), 7);
+                   preg_match('/^(\*{1,3}.*?)(?:\[#([A-Za-z][\w-]*)\]\s*)?$/', $line, $matches) &&
+                   (! isset($matches[2]) || $matches[2] == '')) {
+                       // Generate unique id
+                       $anchor = generate_fixed_heading_anchor_id($matches[1]);
                        $line = rtrim($matches[1]) . ' [#' . $anchor . ']';
                }
        }
@@ -119,6 +117,15 @@ function make_str_rules($source)
        return implode("\n", $lines);
 }
 
+// Generate ID
+function generate_fixed_heading_anchor_id($seed)
+{
+       // A random alphabetic letter + 7 letters of random strings from md()
+       return chr(mt_rand(ord('a'), ord('z'))) .
+               substr(md5(uniqid(substr($seed, 0, 100), TRUE)),
+               mt_rand(0, 24), 7);
+}
+
 // Output to a file
 function file_write($dir, $page, $str, $notimestamp = FALSE)
 {