diff --git a/app/class/encoding.class.php b/app/class/encoding.class.php index ce9f022..31e8cf7 100644 --- a/app/class/encoding.class.php +++ b/app/class/encoding.class.php @@ -36,9 +36,8 @@ POSSIBILITY OF SUCH DAMAGE. * @example https://github.com/neitanod/forceutf8 * @license Revised BSD */ - - -class Encoding { +class Encoding +{ const ICONV_TRANSLIT = "TRANSLIT"; const ICONV_IGNORE = "IGNORE"; @@ -118,18 +117,18 @@ class Encoding { "\xe2\x82\xac" => "\x80", "\xe2\x80\x9a" => "\x82", - "\xc6\x92" => "\x83", + "\xc6\x92" => "\x83", "\xe2\x80\x9e" => "\x84", "\xe2\x80\xa6" => "\x85", "\xe2\x80\xa0" => "\x86", "\xe2\x80\xa1" => "\x87", - "\xcb\x86" => "\x88", + "\xcb\x86" => "\x88", "\xe2\x80\xb0" => "\x89", - "\xc5\xa0" => "\x8a", + "\xc5\xa0" => "\x8a", "\xe2\x80\xb9" => "\x8b", - "\xc5\x92" => "\x8c", + "\xc5\x92" => "\x8c", - "\xc5\xbd" => "\x8e", + "\xc5\xbd" => "\x8e", "\xe2\x80\x98" => "\x91", @@ -139,17 +138,49 @@ class Encoding { "\xe2\x80\xa2" => "\x95", "\xe2\x80\x93" => "\x96", "\xe2\x80\x94" => "\x97", - "\xcb\x9c" => "\x98", + "\xcb\x9c" => "\x98", "\xe2\x84\xa2" => "\x99", - "\xc5\xa1" => "\x9a", + "\xc5\xa1" => "\x9a", "\xe2\x80\xba" => "\x9b", - "\xc5\x93" => "\x9c", + "\xc5\x93" => "\x9c", - "\xc5\xbe" => "\x9e", - "\xc5\xb8" => "\x9f" + "\xc5\xbe" => "\x9e", + "\xc5\xb8" => "\x9f" ); - static function toUTF8($text){ + static function toISO8859($text, $option = self::WITHOUT_ICONV) + { + return self::toWin1252($text, $option); + } + + static function toWin1252($text, $option = self::WITHOUT_ICONV) + { + if (is_array($text)) { + foreach ($text as $k => $v) { + $text[$k] = self::toWin1252($v, $option); + } + return $text; + } elseif (is_string($text)) { + return static::utf8_decode($text, $option); + } else { + return $text; + } + } + + protected static function utf8_decode($text, $option = self::WITHOUT_ICONV) + { + if ($option == self::WITHOUT_ICONV || !function_exists('iconv')) { + $o = utf8_decode( + str_replace(array_keys(self::$utf8ToWin1252), array_values(self::$utf8ToWin1252), self::toUTF8($text)) + ); + } else { + $o = iconv("UTF-8", "Windows-1252" . ($option == self::ICONV_TRANSLIT ? '//TRANSLIT' : ($option == self::ICONV_IGNORE ? '//IGNORE' : '')), $text); + } + return $o; + } + + static function toUTF8($text) + { /** * Function \ForceUTF8\Encoding::toUTF8 * @@ -170,35 +201,33 @@ class Encoding { * 3) when any of these: ðñòó are followed by THREE chars from group B. * * @name toUTF8 - * @param string $text Any string. + * @param string $text Any string. * @return string The same string, UTF8 encoded * */ - if(is_array($text)) - { - foreach($text as $k => $v) - { + if (is_array($text)) { + foreach ($text as $k => $v) { $text[$k] = self::toUTF8($v); } return $text; } - if(!is_string($text)) { + if (!is_string($text)) { return $text; } $max = self::strlen($text); $buf = ""; - for($i = 0; $i < $max; $i++){ + for ($i = 0; $i < $max; $i++) { $c1 = $text{$i}; - if($c1>="\xc0"){ //Should be converted to UTF8, if it's not UTF8 already - $c2 = $i+1 >= $max? "\x00" : $text{$i+1}; - $c3 = $i+2 >= $max? "\x00" : $text{$i+2}; - $c4 = $i+3 >= $max? "\x00" : $text{$i+3}; - if($c1 >= "\xc0" & $c1 <= "\xdf"){ //looks like 2 bytes UTF8 - if($c2 >= "\x80" && $c2 <= "\xbf"){ //yeah, almost sure it's UTF8 already + if ($c1 >= "\xc0") { //Should be converted to UTF8, if it's not UTF8 already + $c2 = $i + 1 >= $max ? "\x00" : $text{$i + 1}; + $c3 = $i + 2 >= $max ? "\x00" : $text{$i + 2}; + $c4 = $i + 3 >= $max ? "\x00" : $text{$i + 3}; + if ($c1 >= "\xc0" & $c1 <= "\xdf") { //looks like 2 bytes UTF8 + if ($c2 >= "\x80" && $c2 <= "\xbf") { //yeah, almost sure it's UTF8 already $buf .= $c1 . $c2; $i++; } else { //not valid UTF8. Convert it. @@ -206,8 +235,8 @@ class Encoding { $cc2 = ($c1 & "\x3f") | "\x80"; $buf .= $cc1 . $cc2; } - } elseif($c1 >= "\xe0" & $c1 <= "\xef"){ //looks like 3 bytes UTF8 - if($c2 >= "\x80" && $c2 <= "\xbf" && $c3 >= "\x80" && $c3 <= "\xbf"){ //yeah, almost sure it's UTF8 already + } elseif ($c1 >= "\xe0" & $c1 <= "\xef") { //looks like 3 bytes UTF8 + if ($c2 >= "\x80" && $c2 <= "\xbf" && $c3 >= "\x80" && $c3 <= "\xbf") { //yeah, almost sure it's UTF8 already $buf .= $c1 . $c2 . $c3; $i = $i + 2; } else { //not valid UTF8. Convert it. @@ -215,8 +244,8 @@ class Encoding { $cc2 = ($c1 & "\x3f") | "\x80"; $buf .= $cc1 . $cc2; } - } elseif($c1 >= "\xf0" & $c1 <= "\xf7"){ //looks like 4 bytes UTF8 - if($c2 >= "\x80" && $c2 <= "\xbf" && $c3 >= "\x80" && $c3 <= "\xbf" && $c4 >= "\x80" && $c4 <= "\xbf"){ //yeah, almost sure it's UTF8 already + } elseif ($c1 >= "\xf0" & $c1 <= "\xf7") { //looks like 4 bytes UTF8 + if ($c2 >= "\x80" && $c2 <= "\xbf" && $c3 >= "\x80" && $c3 <= "\xbf" && $c4 >= "\x80" && $c4 <= "\xbf") { //yeah, almost sure it's UTF8 already $buf .= $c1 . $c2 . $c3 . $c4; $i = $i + 3; } else { //not valid UTF8. Convert it. @@ -229,8 +258,8 @@ class Encoding { $cc2 = (($c1 & "\x3f") | "\x80"); $buf .= $cc1 . $cc2; } - } elseif(($c1 & "\xc0") == "\x80"){ // needs conversion - if(isset(self::$win1252ToUtf8[ord($c1)])) { //found in Windows-1252 special cases + } elseif (($c1 & "\xc0") == "\x80") { // needs conversion + if (isset(self::$win1252ToUtf8[ord($c1)])) { //found in Windows-1252 special cases $buf .= self::$win1252ToUtf8[ord($c1)]; } else { $cc1 = (chr(ord($c1) / 64) | "\xc0"); @@ -244,41 +273,27 @@ class Encoding { return $buf; } - static function toWin1252($text, $option = self::WITHOUT_ICONV) { - if(is_array($text)) { - foreach($text as $k => $v) { - $text[$k] = self::toWin1252($v, $option); - } - return $text; - } elseif(is_string($text)) { - return static::utf8_decode($text, $option); - } else { - return $text; - } + protected static function strlen($text) + { + return (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload')) & 2) ? + mb_strlen($text, '8bit') : strlen($text); } - static function toISO8859($text, $option = self::WITHOUT_ICONV) { - return self::toWin1252($text, $option); - } - - static function toLatin1($text, $option = self::WITHOUT_ICONV) { - return self::toWin1252($text, $option); - } - - static function fixUTF8($text, $option = self::WITHOUT_ICONV){ - if(is_array($text)) { - foreach($text as $k => $v) { + static function fixUTF8($text, $option = self::WITHOUT_ICONV) + { + if (is_array($text)) { + foreach ($text as $k => $v) { $text[$k] = self::fixUTF8($v, $option); } return $text; } - if(!is_string($text)) { + if (!is_string($text)) { return $text; } $last = ""; - while($last <> $text){ + while ($last <> $text) { $last = $text; $text = self::toUTF8(static::utf8_decode($text, $option)); } @@ -286,7 +301,8 @@ class Encoding { return $text; } - static function UTF8FixWin1252Chars($text){ + static function UTF8FixWin1252Chars($text) + { // If you received an UTF-8 string that was converted from Windows-1252 as it was ISO8859-1 // (ignoring Windows-1252 chars from 80 to 9F) use this function to fix it. // See: http://en.wikipedia.org/wiki/Windows-1252 @@ -294,16 +310,19 @@ class Encoding { return str_replace(array_keys(self::$brokenUtf8ToUtf8), array_values(self::$brokenUtf8ToUtf8), $text); } - static function removeBOM($str=""){ - if(substr($str, 0,3) == pack("CCC",0xef,0xbb,0xbf)) { - $str=substr($str, 3); + static function removeBOM($str = "") + { + if (substr($str, 0, 3) == pack("CCC", 0xef, 0xbb, 0xbf)) { + $str = substr($str, 3); } return $str; } - protected static function strlen($text){ - return (function_exists('mb_strlen') && ((int) ini_get('mbstring.func_overload')) & 2) ? - mb_strlen($text,'8bit') : strlen($text); + public static function encode($encodingLabel, $text) + { + $encodingLabel = self::normalizeEncoding($encodingLabel); + if ($encodingLabel == 'ISO-8859-1') return self::toLatin1($text); + return self::toUTF8($text); } public static function normalizeEncoding($encodingLabel) @@ -312,59 +331,50 @@ class Encoding { $encoding = preg_replace('/[^a-zA-Z0-9\s]/', '', $encoding); $equivalences = array( 'ISO88591' => 'ISO-8859-1', - 'ISO8859' => 'ISO-8859-1', - 'ISO' => 'ISO-8859-1', - 'LATIN1' => 'ISO-8859-1', - 'LATIN' => 'ISO-8859-1', - 'UTF8' => 'UTF-8', - 'UTF' => 'UTF-8', - 'WIN1252' => 'ISO-8859-1', + 'ISO8859' => 'ISO-8859-1', + 'ISO' => 'ISO-8859-1', + 'LATIN1' => 'ISO-8859-1', + 'LATIN' => 'ISO-8859-1', + 'UTF8' => 'UTF-8', + 'UTF' => 'UTF-8', + 'WIN1252' => 'ISO-8859-1', 'WINDOWS1252' => 'ISO-8859-1' ); - if(empty($equivalences[$encoding])){ + if (empty($equivalences[$encoding])) { return 'UTF-8'; } return $equivalences[$encoding]; } - public static function encode($encodingLabel, $text) + static function toLatin1($text, $option = self::WITHOUT_ICONV) { - $encodingLabel = self::normalizeEncoding($encodingLabel); - if($encodingLabel == 'ISO-8859-1') return self::toLatin1($text); - return self::toUTF8($text); - } - - protected static function utf8_decode($text, $option = self::WITHOUT_ICONV) - { - if ($option == self::WITHOUT_ICONV || !function_exists('iconv')) { - $o = utf8_decode( - str_replace(array_keys(self::$utf8ToWin1252), array_values(self::$utf8ToWin1252), self::toUTF8($text)) - ); - } else { - $o = iconv("UTF-8", "Windows-1252" . ($option == self::ICONV_TRANSLIT ? '//TRANSLIT' : ($option == self::ICONV_IGNORE ? '//IGNORE' : '')), $text); - } - return $o; + return self::toWin1252($text, $option); } /****/ - public static function destructionH4x0RChaine($chaine){ + public static function destructionH4x0RChaine($chaine) + { return preg_replace('#[\x00-\x1F\x7F-\x9F/\\\\]#', '', $chaine); } - public static function protectionDoubleQuote($chaine){ + public static function protectionDoubleQuote($chaine) + { $chaine = preg_replace('/"(\w+)"/', '« ${1} »', $chaine); $chaine = preg_replace('#"#', '_', $chaine); return $chaine; } - public static function protectionSimpleQuote($chaine){ + + public static function protectionSimpleQuote($chaine) + { $chaine = preg_replace("#'#", '_', $chaine); return $chaine; } - public static function myUrlEncode($string) { + public static function myUrlEncode($string) + { $replacements = array('%21', '%2A', '%27', '%28', '%29', '%3B', '%3A', '%40', '%26', '%3D'/*, '%2B'*/, '%24', '%2C', '%2F', '%3F', '%25', '%23', '%5B', '%5D'); $entities = array('!', '*', "'", "(", ")", ";", ":", "@", "&", "="/*, "+"*/, "$", ",", "/", "?", "%", "#", "[", "]"); $string = urlencode($string); @@ -372,7 +382,8 @@ class Encoding { return $string; } - public static function myUrlDecode($string) { + public static function myUrlDecode($string) + { $entities = array('%21', '%2A', '%27', '%28', '%29', '%3B', '%3A', '%40', '%26', '%3D'/*, '%2B'*/, '%24', '%2C', '%2F', '%3F', '%25', '%23', '%5B', '%5D'); $replacements = array('!', '*', "'", "(", ")", ";", ":", "@", "&", "="/*, "+"*/, "$", ",", "/", "?", "%", "#", "[", "]"); $string = str_replace($entities, $replacements, $string); diff --git a/app/class/password.class.php b/app/class/password.class.php index a18abc2..93ec641 100644 --- a/app/class/password.class.php +++ b/app/class/password.class.php @@ -27,18 +27,19 @@ namespace { * Hash the password using the specified algorithm * * @param string $password The password to hash - * @param int $algo The algorithm to use (Defined by PASSWORD_* constants) - * @param array $options The options for the algorithm to use + * @param int $algo The algorithm to use (Defined by PASSWORD_* constants) + * @param array $options The options for the algorithm to use * * @return string|false The hashed password, or false on error. */ - function password_hash($password, $algo, array $options = array()) { + function password_hash($password, $algo, array $options = array()) + { if (!function_exists('crypt')) { trigger_error("Crypt must be loaded for password_hash to function", E_USER_WARNING); return null; } if (is_null($password) || is_int($password)) { - $password = (string) $password; + $password = (string)$password; } if (!is_string($password)) { trigger_error("password_hash(): Password must be a string", E_USER_WARNING); @@ -53,7 +54,7 @@ namespace { case PASSWORD_BCRYPT: $cost = PASSWORD_BCRYPT_DEFAULT_COST; if (isset($options['cost'])) { - $cost = (int) $options['cost']; + $cost = (int)$options['cost']; if ($cost < 4 || $cost > 31) { trigger_error(sprintf("password_hash(): Invalid bcrypt cost parameter specified: %d", $cost), E_USER_WARNING); return null; @@ -79,11 +80,11 @@ namespace { case 'integer': case 'double': case 'string': - $salt = (string) $options['salt']; + $salt = (string)$options['salt']; break; case 'object': if (method_exists($options['salt'], '__tostring')) { - $salt = (string) $options['salt']; + $salt = (string)$options['salt']; break; } case 'array': @@ -180,7 +181,8 @@ namespace { * * @return array The array of information about the hash. */ - function password_get_info($hash) { + function password_get_info($hash) + { $return = array( 'algo' => 0, 'algoName' => 'unknown', @@ -200,20 +202,21 @@ namespace { * * If the answer is true, after validating the password using password_verify, rehash it. * - * @param string $hash The hash to test - * @param int $algo The algorithm used for new password hashes - * @param array $options The options array passed to password_hash + * @param string $hash The hash to test + * @param int $algo The algorithm used for new password hashes + * @param array $options The options array passed to password_hash * * @return boolean True if the password needs to be rehashed. */ - function password_needs_rehash($hash, $algo, array $options = array()) { + function password_needs_rehash($hash, $algo, array $options = array()) + { $info = password_get_info($hash); - if ($info['algo'] !== (int) $algo) { + if ($info['algo'] !== (int)$algo) { return true; } switch ($algo) { case PASSWORD_BCRYPT: - $cost = isset($options['cost']) ? (int) $options['cost'] : PASSWORD_BCRYPT_DEFAULT_COST; + $cost = isset($options['cost']) ? (int)$options['cost'] : PASSWORD_BCRYPT_DEFAULT_COST; if ($cost !== $info['options']['cost']) { return true; } @@ -226,11 +229,12 @@ namespace { * Verify a password against a hash using a timing attack resistant approach * * @param string $password The password to verify - * @param string $hash The hash to verify against + * @param string $hash The hash to verify against * * @return boolean If the password matches the hash */ - function password_verify($password, $hash) { + function password_verify($password, $hash) + { if (!function_exists('crypt')) { trigger_error("Crypt must be loaded for password_verify to function", E_USER_WARNING); return false; @@ -264,10 +268,11 @@ namespace PasswordCompat\binary { * * @param string $binary_string The input string * - * @internal * @return int The number of bytes + * @internal */ - function _strlen($binary_string) { + function _strlen($binary_string) + { if (function_exists('mb_strlen')) { return mb_strlen($binary_string, '8bit'); } @@ -277,16 +282,17 @@ namespace PasswordCompat\binary { /** * Get a substring based on byte limits * + * @param string $binary_string The input string + * @param int $start + * @param int $length + * + * @return string The substring + * @internal * @see _strlen() * - * @param string $binary_string The input string - * @param int $start - * @param int $length - * - * @internal - * @return string The substring */ - function _substr($binary_string, $start, $length) { + function _substr($binary_string, $start, $length) + { if (function_exists('mb_substr')) { return mb_substr($binary_string, $start, $length, '8bit'); } @@ -298,7 +304,8 @@ namespace PasswordCompat\binary { * * @return boolean the check result */ - function check() { + function check() + { static $pass = NULL; if (is_null($pass)) { diff --git a/app/layout/footer.php b/app/layout/footer.php index 0315e48..09db966 100644 --- a/app/layout/footer.php +++ b/app/layout/footer.php @@ -1,24 +1,27 @@ - - - - - + - - - + + + + + + diff --git a/app/layout/header.php b/app/layout/header.php index 2e9d0b8..8305873 100644 --- a/app/layout/header.php +++ b/app/layout/header.php @@ -1,72 +1,80 @@ - - - + + + - + + - + - - - - - - - - - - - //a utiliser avec un syteme de classement utilisateur - - //link: http://en.wikipedia.org/wiki/Smart_tag_%28Microsoft%29 - - //link: https://support.google.com/customsearch/answer/2595557?hl=fr - - */ - ?> - - - - - - - - - - + + + + + + + + + + + //a utiliser avec un syteme de classement utilisateur + + //link: http://en.wikipedia.org/wiki/Smart_tag_%28Microsoft%29 + + //link: https://support.google.com/customsearch/answer/2595557?hl=fr + + */ + ?> - - + + + + + + + + + - <?php echo $title ?> + + - - + <?php echo $title ?> - + + - + - + + + - - - - + + - - - + + diff --git a/app/layout/layout_codage.phtml b/app/layout/layout_codage.phtml index 0df85cc..d3573f7 100644 --- a/app/layout/layout_codage.phtml +++ b/app/layout/layout_codage.phtml @@ -1,21 +1,21 @@ - - - - -


- -


- - - - - + + + diff --git a/app/page/TINTERNET-chatbot.phtml b/app/page/TINTERNET-chatbot.phtml index c4cd6b0..ef7f214 100644 --- a/app/page/TINTERNET-chatbot.phtml +++ b/app/page/TINTERNET-chatbot.phtml @@ -2,26 +2,29 @@ -
+
- +
- +
- +
-

+

La veille Mattermost as été correctement transmis.

"; - }else{ + } else { echo "

La veille Mattermost n'as pas été transmis. Veuillez vérifiez les informations que vous avez saisies ...

"; } } diff --git a/app/page/error-brute-force-attack.php b/app/page/error-brute-force-attack.php index 6323a7d..d4a1444 100644 --- a/app/page/error-brute-force-attack.php +++ b/app/page/error-brute-force-attack.php @@ -1,5 +1,5 @@ -

What You will is Not found :(

-

Désolé, mais ce que vous venez de faire ne semble pas exister.

-

Il semblerait que ce soit:

- - +
+ + + + + +
+ + \ No newline at end of file diff --git a/app/page/error.php b/app/page/error.php index 3c24d16..b553dca 100644 --- a/app/page/error.php +++ b/app/page/error.php @@ -1,5 +1,5 @@ -

Not found :(

-

Désolé, mais la page que vous recherchez ne semble pas exister.

-

Il semblerait que ce soit:

- - +
+ + + + + +
+ +

Voici quand même les quelques pages qui sont valables d'être cherchées

diff --git a/app/parameters.php b/app/parameters.php index 5cf4189..2f27d48 100644 --- a/app/parameters.php +++ b/app/parameters.php @@ -1,6 +1,6 @@ xpath("/brute-force-attack/attempt/from")) { +$attempts = simplexml_load_file(APPLICATION_PATH . DIRECTORY_SEPARATOR . "bruteforce-logger.xml", "SimpleXMLElement", LIBXML_NOCDATA); +if ($attempts->xpath("/brute-force-attack/attempt/from")) { foreach ($attempts->xpath("/brute-force-attack/attempt/from") as $attempt) { // Récupération des ip et test de corrélation actuelle if ($attempt['from'] == $_SERVER['REMOTE_ADDR']) { @@ -18,48 +18,48 @@ if($attempts->xpath("/brute-force-attack/attempt/from")) { /**/ /* ITERATION D'UNE POSSIBLE ATTAQUE BRUTE FORCE SUR UNE PAGE DE TRAITEMENT */ - if(isset($_SESSION['brute-force-attempt'])) { - $_SESSION['brute-force-attempt']++; - }else{ - $_SESSION['brute-force-attempt']=1; - } +if (isset($_SESSION['brute-force-attempt'])) { + $_SESSION['brute-force-attempt']++; +} else { + $_SESSION['brute-force-attempt'] = 1; +} /**/ /* TESTS PERMETTANT DE DEFINIR CERTAINS CAS COMME ETANT DANGEREUX*/ -if ( !isset($_SESSION['current-ip']) - || !isset($_SESSION['current-time']) ){ +if (!isset($_SESSION['current-ip']) + || !isset($_SESSION['current-time'])) { //Ce test permet d'identifier ceux qui veulent faire un traitement sans provenir d'une page de formulaire ob_start(); - require_once PAGES_PATH . DIRECTORY_SEPARATOR . 'error.php' ; - require_once PAGES_PATH . DIRECTORY_SEPARATOR . 'error.phtml' ; + require_once PAGES_PATH . DIRECTORY_SEPARATOR . 'error.php'; + require_once PAGES_PATH . DIRECTORY_SEPARATOR . 'error.phtml'; $content = ob_get_contents(); ob_end_clean(); require_once LAYOUT_PATH . DIRECTORY_SEPARATOR . 'layout-codage.phtml'; die(); -}else{ - - if( $_SESSION['last-ip'] !== $_SERVER['REMOTE_ADDR'] - || $_SESSION['last-time'] > $_SERVER['REQUEST_TIME'] ){ +} else { - //ce test permet de verifier que le demandeur est bien le meme que celui qui provient d'un formulaire + if ($_SESSION['last-ip'] !== $_SERVER['REMOTE_ADDR'] + || $_SESSION['last-time'] > $_SERVER['REQUEST_TIME']) { + + //ce test permet de verifier que le demandeur est bien le meme que celui qui provient d'un formulaire ob_start(); - require_once PAGES_PATH . DIRECTORY_SEPARATOR . 'error.php' ; - require_once PAGES_PATH . DIRECTORY_SEPARATOR . 'error.phtml' ; + require_once PAGES_PATH . DIRECTORY_SEPARATOR . 'error.php'; + require_once PAGES_PATH . DIRECTORY_SEPARATOR . 'error.phtml'; $content = ob_get_contents(); ob_end_clean(); require_once LAYOUT_PATH . DIRECTORY_SEPARATOR . 'layout-codage.phtml'; die(); - } + } - if( $_SESSION['brute-force-attempt'] > 5 ){ + if ($_SESSION['brute-force-attempt'] > 5) { - //ce test permet de logger une attaque brute force sur un formulaire + //ce test permet de logger une attaque brute force sur un formulaire $dom = new DOMDocument; - $dom->load(APPLICATION_PATH . DIRECTORY_SEPARATOR ."bruteforce-logger.xml"); + $dom->load(APPLICATION_PATH . DIRECTORY_SEPARATOR . "bruteforce-logger.xml"); $noeudBruteForce = $dom->getElementsByTagName("brute-force-attack")->item(0); @@ -76,12 +76,12 @@ if ( !isset($_SESSION['current-ip']) $attemptNode->appendChild($ipNode); $noeudBruteForce->appendChild($attemptNode); - $dom->save(APPLICATION_PATH . DIRECTORY_SEPARATOR ."bruteforce-logger.xml"); + $dom->save(APPLICATION_PATH . DIRECTORY_SEPARATOR . "bruteforce-logger.xml"); //pour chaque page et mise en tampon ob_start(); - require_once PAGES_PATH . DIRECTORY_SEPARATOR . 'error-brute-force-attack.php' ; - require_once PAGES_PATH . DIRECTORY_SEPARATOR . 'error-brute-force-attack.phtml' ; + require_once PAGES_PATH . DIRECTORY_SEPARATOR . 'error-brute-force-attack.php'; + require_once PAGES_PATH . DIRECTORY_SEPARATOR . 'error-brute-force-attack.phtml'; $content = ob_get_contents(); ob_end_clean(); require_once LAYOUT_PATH . DIRECTORY_SEPARATOR . 'layout-codage.phtml'; @@ -89,9 +89,9 @@ if ( !isset($_SESSION['current-ip']) } - //mise en tampon des données courante de combat - $_SESSION['last-ip'] = $_SESSION['current-ip']; - $_SESSION['last-time'] = $_SESSION['current-time']; + //mise en tampon des données courante de combat + $_SESSION['last-ip'] = $_SESSION['current-ip']; + $_SESSION['last-time'] = $_SESSION['current-time']; } /**/ diff --git a/public/index.php b/public/index.php index f130c50..089b99c 100644 --- a/public/index.php +++ b/public/index.php @@ -1,20 +1,20 @@ JsonML - * - markdown (String): markdown string to parse - * - dialect (String | Dialect): the dialect to use, defaults to gruber - * - * Parse `markdown` and return a markdown document as a Markdown.JsonML tree. - **/ -expose.parse = function( source, dialect ) { - // dialect will default if undefined - var md = new Markdown( dialect ); - return md.toTree( source ); -}; + /** + * parse( markdown, [dialect] ) -> JsonML + * - markdown (String): markdown string to parse + * - dialect (String | Dialect): the dialect to use, defaults to gruber + * + * Parse `markdown` and return a markdown document as a Markdown.JsonML tree. + **/ + expose.parse = function (source, dialect) { + // dialect will default if undefined + var md = new Markdown(dialect); + return md.toTree(source); + }; -/** - * toHTML( markdown, [dialect] ) -> String - * toHTML( md_tree ) -> String - * - markdown (String): markdown string to parse - * - md_tree (Markdown.JsonML): parsed markdown tree - * - * Take markdown (either as a string or as a JsonML tree) and run it through - * [[toHTMLTree]] then turn it into a well-formated HTML fragment. - **/ -expose.toHTML = function toHTML( source , dialect , options ) { - var input = expose.toHTMLTree( source , dialect , options ); + /** + * toHTML( markdown, [dialect] ) -> String + * toHTML( md_tree ) -> String + * - markdown (String): markdown string to parse + * - md_tree (Markdown.JsonML): parsed markdown tree + * + * Take markdown (either as a string or as a JsonML tree) and run it through + * [[toHTMLTree]] then turn it into a well-formated HTML fragment. + **/ + expose.toHTML = function toHTML(source, dialect, options) { + var input = expose.toHTMLTree(source, dialect, options); - return expose.renderJsonML( input ); -}; + return expose.renderJsonML(input); + }; -/** - * toHTMLTree( markdown, [dialect] ) -> JsonML - * toHTMLTree( md_tree ) -> JsonML - * - markdown (String): markdown string to parse - * - dialect (String | Dialect): the dialect to use, defaults to gruber - * - md_tree (Markdown.JsonML): parsed markdown tree - * - * Turn markdown into HTML, represented as a JsonML tree. If a string is given - * to this function, it is first parsed into a markdown tree by calling - * [[parse]]. - **/ -expose.toHTMLTree = function toHTMLTree( input, dialect , options ) { - // convert string input to an MD tree - if ( typeof input ==="string" ) input = this.parse( input, dialect ); + /** + * toHTMLTree( markdown, [dialect] ) -> JsonML + * toHTMLTree( md_tree ) -> JsonML + * - markdown (String): markdown string to parse + * - dialect (String | Dialect): the dialect to use, defaults to gruber + * - md_tree (Markdown.JsonML): parsed markdown tree + * + * Turn markdown into HTML, represented as a JsonML tree. If a string is given + * to this function, it is first parsed into a markdown tree by calling + * [[parse]]. + **/ + expose.toHTMLTree = function toHTMLTree(input, dialect, options) { + // convert string input to an MD tree + if (typeof input === "string") input = this.parse(input, dialect); - // Now convert the MD tree to an HTML tree + // Now convert the MD tree to an HTML tree - // remove references from the tree - var attrs = extract_attr( input ), - refs = {}; + // remove references from the tree + var attrs = extract_attr(input), + refs = {}; - if ( attrs && attrs.references ) { - refs = attrs.references; - } + if (attrs && attrs.references) { + refs = attrs.references; + } - var html = convert_tree_to_html( input, refs , options ); - merge_text_nodes( html ); - return html; -}; + var html = convert_tree_to_html(input, refs, options); + merge_text_nodes(html); + return html; + }; // For Spidermonkey based engines -function mk_block_toSource() { - return "Markdown.mk_block( " + - uneval(this.toString()) + - ", " + - uneval(this.trailing) + - ", " + - uneval(this.lineNumber) + - " )"; -} + function mk_block_toSource() { + return "Markdown.mk_block( " + + uneval(this.toString()) + + ", " + + uneval(this.trailing) + + ", " + + uneval(this.lineNumber) + + " )"; + } // node -function mk_block_inspect() { - var util = require("util"); - return "Markdown.mk_block( " + - util.inspect(this.toString()) + - ", " + - util.inspect(this.trailing) + - ", " + - util.inspect(this.lineNumber) + - " )"; + function mk_block_inspect() { + var util = require("util"); + return "Markdown.mk_block( " + + util.inspect(this.toString()) + + ", " + + util.inspect(this.trailing) + + ", " + + util.inspect(this.lineNumber) + + " )"; -} + } -var mk_block = Markdown.mk_block = function(block, trail, line) { - // Be helpful for default case in tests. - if ( arguments.length == 1 ) trail = "\n\n"; + var mk_block = Markdown.mk_block = function (block, trail, line) { + // Be helpful for default case in tests. + if (arguments.length == 1) trail = "\n\n"; - var s = new String(block); - s.trailing = trail; - // To make it clear its not just a string - s.inspect = mk_block_inspect; - s.toSource = mk_block_toSource; + var s = new String(block); + s.trailing = trail; + // To make it clear its not just a string + s.inspect = mk_block_inspect; + s.toSource = mk_block_toSource; - if ( line != undefined ) - s.lineNumber = line; + if (line != undefined) + s.lineNumber = line; - return s; -}; + return s; + }; -function count_lines( str ) { - var n = 0, i = -1; - while ( ( i = str.indexOf("\n", i + 1) ) !== -1 ) n++; - return n; -} + function count_lines(str) { + var n = 0, i = -1; + while ((i = str.indexOf("\n", i + 1)) !== -1) n++; + return n; + } // Internal - split source into rough blocks -Markdown.prototype.split_blocks = function splitBlocks( input, startLine ) { - input = input.replace(/(\r\n|\n|\r)/g, "\n"); - // [\s\S] matches _anything_ (newline or space) - // [^] is equivalent but doesn't work in IEs. - var re = /([\s\S]+?)($|\n#|\n(?:\s*\n|$)+)/g, - blocks = [], - m; + Markdown.prototype.split_blocks = function splitBlocks(input, startLine) { + input = input.replace(/(\r\n|\n|\r)/g, "\n"); + // [\s\S] matches _anything_ (newline or space) + // [^] is equivalent but doesn't work in IEs. + var re = /([\s\S]+?)($|\n#|\n(?:\s*\n|$)+)/g, + blocks = [], + m; - var line_no = 1; + var line_no = 1; - if ( ( m = /^(\s*\n)/.exec(input) ) != null ) { - // skip (but count) leading blank lines - line_no += count_lines( m[0] ); - re.lastIndex = m[0].length; - } + if ((m = /^(\s*\n)/.exec(input)) != null) { + // skip (but count) leading blank lines + line_no += count_lines(m[0]); + re.lastIndex = m[0].length; + } - while ( ( m = re.exec(input) ) !== null ) { - if (m[2] == "\n#") { - m[2] = "\n"; - re.lastIndex--; - } - blocks.push( mk_block( m[1], m[2], line_no ) ); - line_no += count_lines( m[0] ); - } + while ((m = re.exec(input)) !== null) { + if (m[2] == "\n#") { + m[2] = "\n"; + re.lastIndex--; + } + blocks.push(mk_block(m[1], m[2], line_no)); + line_no += count_lines(m[0]); + } - return blocks; -}; + return blocks; + }; -/** - * Markdown#processBlock( block, next ) -> undefined | [ JsonML, ... ] - * - block (String): the block to process - * - next (Array): the following blocks - * - * Process `block` and return an array of JsonML nodes representing `block`. - * - * It does this by asking each block level function in the dialect to process - * the block until one can. Succesful handling is indicated by returning an - * array (with zero or more JsonML nodes), failure by a false value. - * - * Blocks handlers are responsible for calling [[Markdown#processInline]] - * themselves as appropriate. - * - * If the blocks were split incorrectly or adjacent blocks need collapsing you - * can adjust `next` in place using shift/splice etc. - * - * If any of this default behaviour is not right for the dialect, you can - * define a `__call__` method on the dialect that will get invoked to handle - * the block processing. - */ -Markdown.prototype.processBlock = function processBlock( block, next ) { - var cbs = this.dialect.block, - ord = cbs.__order__; + /** + * Markdown#processBlock( block, next ) -> undefined | [ JsonML, ... ] + * - block (String): the block to process + * - next (Array): the following blocks + * + * Process `block` and return an array of JsonML nodes representing `block`. + * + * It does this by asking each block level function in the dialect to process + * the block until one can. Succesful handling is indicated by returning an + * array (with zero or more JsonML nodes), failure by a false value. + * + * Blocks handlers are responsible for calling [[Markdown#processInline]] + * themselves as appropriate. + * + * If the blocks were split incorrectly or adjacent blocks need collapsing you + * can adjust `next` in place using shift/splice etc. + * + * If any of this default behaviour is not right for the dialect, you can + * define a `__call__` method on the dialect that will get invoked to handle + * the block processing. + */ + Markdown.prototype.processBlock = function processBlock(block, next) { + var cbs = this.dialect.block, + ord = cbs.__order__; - if ( "__call__" in cbs ) { - return cbs.__call__.call(this, block, next); - } + if ("__call__" in cbs) { + return cbs.__call__.call(this, block, next); + } - for ( var i = 0; i < ord.length; i++ ) { - //D:this.debug( "Testing", ord[i] ); - var res = cbs[ ord[i] ].call( this, block, next ); - if ( res ) { - //D:this.debug(" matched"); - if ( !isArray(res) || ( res.length > 0 && !( isArray(res[0]) ) ) ) - this.debug(ord[i], "didn't return a proper array"); - //D:this.debug( "" ); - return res; - } - } + for (var i = 0; i < ord.length; i++) { + //D:this.debug( "Testing", ord[i] ); + var res = cbs[ord[i]].call(this, block, next); + if (res) { + //D:this.debug(" matched"); + if (!isArray(res) || (res.length > 0 && !(isArray(res[0])))) + this.debug(ord[i], "didn't return a proper array"); + //D:this.debug( "" ); + return res; + } + } - // Uhoh! no match! Should we throw an error? - return []; -}; + // Uhoh! no match! Should we throw an error? + return []; + }; -Markdown.prototype.processInline = function processInline( block ) { - return this.dialect.inline.__call__.call( this, String( block ) ); -}; + Markdown.prototype.processInline = function processInline(block) { + return this.dialect.inline.__call__.call(this, String(block)); + }; -/** - * Markdown#toTree( source ) -> JsonML - * - source (String): markdown source to parse - * - * Parse `source` into a JsonML tree representing the markdown document. - **/ + /** + * Markdown#toTree( source ) -> JsonML + * - source (String): markdown source to parse + * + * Parse `source` into a JsonML tree representing the markdown document. + **/ // custom_tree means set this.tree to `custom_tree` and restore old value on return -Markdown.prototype.toTree = function toTree( source, custom_root ) { - var blocks = source instanceof Array ? source : this.split_blocks( source ); + Markdown.prototype.toTree = function toTree(source, custom_root) { + var blocks = source instanceof Array ? source : this.split_blocks(source); - // Make tree a member variable so its easier to mess with in extensions - var old_tree = this.tree; - try { - this.tree = custom_root || this.tree || [ "markdown" ]; + // Make tree a member variable so its easier to mess with in extensions + var old_tree = this.tree; + try { + this.tree = custom_root || this.tree || ["markdown"]; - blocks: - while ( blocks.length ) { - var b = this.processBlock( blocks.shift(), blocks ); + blocks: + while (blocks.length) { + var b = this.processBlock(blocks.shift(), blocks); - // Reference blocks and the like won't return any content - if ( !b.length ) continue blocks; + // Reference blocks and the like won't return any content + if (!b.length) continue blocks; - this.tree.push.apply( this.tree, b ); - } - return this.tree; - } - finally { - if ( custom_root ) { - this.tree = old_tree; - } - } -}; + this.tree.push.apply(this.tree, b); + } + return this.tree; + } finally { + if (custom_root) { + this.tree = old_tree; + } + } + }; // Noop by default -Markdown.prototype.debug = function () { - var args = Array.prototype.slice.call( arguments); - args.unshift(this.debug_indent); - if ( typeof print !== "undefined" ) - print.apply( print, args ); - if ( typeof console !== "undefined" && typeof console.log !== "undefined" ) - console.log.apply( null, args ); -} - -Markdown.prototype.loop_re_over_block = function( re, block, cb ) { - // Dont use /g regexps with this - var m, - b = block.valueOf(); - - while ( b.length && (m = re.exec(b) ) != null ) { - b = b.substr( m[0].length ); - cb.call(this, m); - } - return b; -}; - -/** - * Markdown.dialects - * - * Namespace of built-in dialects. - **/ -Markdown.dialects = {}; - -/** - * Markdown.dialects.Gruber - * - * The default dialect that follows the rules set out by John Gruber's - * markdown.pl as closely as possible. Well actually we follow the behaviour of - * that script which in some places is not exactly what the syntax web page - * says. - **/ -Markdown.dialects.Gruber = { - block: { - atxHeader: function atxHeader( block, next ) { - var m = block.match( /^(#{1,6})\s*(.*?)\s*#*\s*(?:\n|$)/ ); - - if ( !m ) return undefined; - - var header = [ "header", { level: m[ 1 ].length } ]; - Array.prototype.push.apply(header, this.processInline(m[ 2 ])); - - if ( m[0].length < block.length ) - next.unshift( mk_block( block.substr( m[0].length ), block.trailing, block.lineNumber + 2 ) ); - - return [ header ]; - }, - - setextHeader: function setextHeader( block, next ) { - var m = block.match( /^(.*)\n([-=])\2\2+(?:\n|$)/ ); - - if ( !m ) return undefined; - - var level = ( m[ 2 ] === "=" ) ? 1 : 2; - var header = [ "header", { level : level }, m[ 1 ] ]; - - if ( m[0].length < block.length ) - next.unshift( mk_block( block.substr( m[0].length ), block.trailing, block.lineNumber + 2 ) ); - - return [ header ]; - }, - - code: function code( block, next ) { - // | Foo - // |bar - // should be a code block followed by a paragraph. Fun - // - // There might also be adjacent code block to merge. - - var ret = [], - re = /^(?: {0,3}\t| {4})(.*)\n?/, - lines; - - // 4 spaces + content - if ( !block.match( re ) ) return undefined; - - block_search: - do { - // Now pull out the rest of the lines - var b = this.loop_re_over_block( - re, block.valueOf(), function( m ) { ret.push( m[1] ); } ); - - if ( b.length ) { - // Case alluded to in first comment. push it back on as a new block - next.unshift( mk_block(b, block.trailing) ); - break block_search; - } - else if ( next.length ) { - // Check the next block - it might be code too - if ( !next[0].match( re ) ) break block_search; - - // Pull how how many blanks lines follow - minus two to account for .join - ret.push ( block.trailing.replace(/[^\n]/g, "").substring(2) ); - - block = next.shift(); - } - else { - break block_search; - } - } while ( true ); - - return [ [ "code_block", ret.join("\n") ] ]; - }, - - horizRule: function horizRule( block, next ) { - // this needs to find any hr in the block to handle abutting blocks - var m = block.match( /^(?:([\s\S]*?)\n)?[ \t]*([-_*])(?:[ \t]*\2){2,}[ \t]*(?:\n([\s\S]*))?$/ ); - - if ( !m ) { - return undefined; - } - - var jsonml = [ [ "hr" ] ]; - - // if there's a leading abutting block, process it - if ( m[ 1 ] ) { - jsonml.unshift.apply( jsonml, this.processBlock( m[ 1 ], [] ) ); - } - - // if there's a trailing abutting block, stick it into next - if ( m[ 3 ] ) { - next.unshift( mk_block( m[ 3 ] ) ); - } - - return jsonml; - }, - - // There are two types of lists. Tight and loose. Tight lists have no whitespace - // between the items (and result in text just in the
  • ) and loose lists, - // which have an empty line between list items, resulting in (one or more) - // paragraphs inside the
  • . - // - // There are all sorts weird edge cases about the original markdown.pl's - // handling of lists: - // - // * Nested lists are supposed to be indented by four chars per level. But - // if they aren't, you can get a nested list by indenting by less than - // four so long as the indent doesn't match an indent of an existing list - // item in the 'nest stack'. - // - // * The type of the list (bullet or number) is controlled just by the - // first item at the indent. Subsequent changes are ignored unless they - // are for nested lists - // - lists: (function( ) { - // Use a closure to hide a few variables. - var any_list = "[*+-]|\\d+\\.", - bullet_list = /[*+-]/, - number_list = /\d+\./, - // Capture leading indent as it matters for determining nested lists. - is_list_re = new RegExp( "^( {0,3})(" + any_list + ")[ \t]+" ), - indent_re = "(?: {0,3}\\t| {4})"; - - // TODO: Cache this regexp for certain depths. - // Create a regexp suitable for matching an li for a given stack depth - function regex_for_depth( depth ) { - - return new RegExp( - // m[1] = indent, m[2] = list_type - "(?:^(" + indent_re + "{0," + depth + "} {0,3})(" + any_list + ")\\s+)|" + - // m[3] = cont - "(^" + indent_re + "{0," + (depth-1) + "}[ ]{0,4})" - ); - } - function expand_tab( input ) { - return input.replace( / {0,3}\t/g, " " ); - } - - // Add inline content `inline` to `li`. inline comes from processInline - // so is an array of content - function add(li, loose, inline, nl) { - if ( loose ) { - li.push( [ "para" ].concat(inline) ); - return; - } - // Hmmm, should this be any block level element or just paras? - var add_to = li[li.length -1] instanceof Array && li[li.length - 1][0] == "para" - ? li[li.length -1] - : li; - - // If there is already some content in this list, add the new line in - if ( nl && li.length > 1 ) inline.unshift(nl); - - for ( var i = 0; i < inline.length; i++ ) { - var what = inline[i], - is_str = typeof what == "string"; - if ( is_str && add_to.length > 1 && typeof add_to[add_to.length-1] == "string" ) { - add_to[ add_to.length-1 ] += what; - } - else { - add_to.push( what ); - } - } - } - - // contained means have an indent greater than the current one. On - // *every* line in the block - function get_contained_blocks( depth, blocks ) { - - var re = new RegExp( "^(" + indent_re + "{" + depth + "}.*?\\n?)*$" ), - replace = new RegExp("^" + indent_re + "{" + depth + "}", "gm"), - ret = []; - - while ( blocks.length > 0 ) { - if ( re.exec( blocks[0] ) ) { - var b = blocks.shift(), - // Now remove that indent - x = b.replace( replace, ""); - - ret.push( mk_block( x, b.trailing, b.lineNumber ) ); - } - else { - break; - } - } - return ret; - } - - // passed to stack.forEach to turn list items up the stack into paras - function paragraphify(s, i, stack) { - var list = s.list; - var last_li = list[list.length-1]; - - if ( last_li[1] instanceof Array && last_li[1][0] == "para" ) { - return; - } - if ( i + 1 == stack.length ) { - // Last stack frame - // Keep the same array, but replace the contents - last_li.push( ["para"].concat( last_li.splice(1, last_li.length - 1) ) ); - } - else { - var sublist = last_li.pop(); - last_li.push( ["para"].concat( last_li.splice(1, last_li.length - 1) ), sublist ); - } - } - - // The matcher function - return function( block, next ) { - var m = block.match( is_list_re ); - if ( !m ) return undefined; - - function make_list( m ) { - var list = bullet_list.exec( m[2] ) - ? ["bulletlist"] - : ["numberlist"]; - - stack.push( { list: list, indent: m[1] } ); - return list; - } - - - var stack = [], // Stack of lists for nesting. - list = make_list( m ), - last_li, - loose = false, - ret = [ stack[0].list ], - i; - - // Loop to search over block looking for inner block elements and loose lists - loose_search: - while ( true ) { - // Split into lines preserving new lines at end of line - var lines = block.split( /(?=\n)/ ); - - // We have to grab all lines for a li and call processInline on them - // once as there are some inline things that can span lines. - var li_accumulate = ""; - - // Loop over the lines in this block looking for tight lists. - tight_search: - for ( var line_no = 0; line_no < lines.length; line_no++ ) { - var nl = "", - l = lines[line_no].replace(/^\n/, function(n) { nl = n; return ""; }); - - // TODO: really should cache this - var line_re = regex_for_depth( stack.length ); - - m = l.match( line_re ); - //print( "line:", uneval(l), "\nline match:", uneval(m) ); - - // We have a list item - if ( m[1] !== undefined ) { - // Process the previous list item, if any - if ( li_accumulate.length ) { - add( last_li, loose, this.processInline( li_accumulate ), nl ); - // Loose mode will have been dealt with. Reset it - loose = false; - li_accumulate = ""; - } - - m[1] = expand_tab( m[1] ); - var wanted_depth = Math.floor(m[1].length/4)+1; - //print( "want:", wanted_depth, "stack:", stack.length); - if ( wanted_depth > stack.length ) { - // Deep enough for a nested list outright - //print ( "new nested list" ); - list = make_list( m ); - last_li.push( list ); - last_li = list[1] = [ "listitem" ]; - } - else { - // We aren't deep enough to be strictly a new level. This is - // where Md.pl goes nuts. If the indent matches a level in the - // stack, put it there, else put it one deeper then the - // wanted_depth deserves. - var found = false; - for ( i = 0; i < stack.length; i++ ) { - if ( stack[ i ].indent != m[1] ) continue; - list = stack[ i ].list; - stack.splice( i+1, stack.length - (i+1) ); - found = true; - break; - } - - if (!found) { - //print("not found. l:", uneval(l)); - wanted_depth++; - if ( wanted_depth <= stack.length ) { - stack.splice(wanted_depth, stack.length - wanted_depth); - //print("Desired depth now", wanted_depth, "stack:", stack.length); - list = stack[wanted_depth-1].list; - //print("list:", uneval(list) ); - } - else { - //print ("made new stack for messy indent"); - list = make_list(m); - last_li.push(list); - } - } - - //print( uneval(list), "last", list === stack[stack.length-1].list ); - last_li = [ "listitem" ]; - list.push(last_li); - } // end depth of shenegains - nl = ""; - } - - // Add content - if ( l.length > m[0].length ) { - li_accumulate += nl + l.substr( m[0].length ); - } - } // tight_search - - if ( li_accumulate.length ) { - add( last_li, loose, this.processInline( li_accumulate ), nl ); - // Loose mode will have been dealt with. Reset it - loose = false; - li_accumulate = ""; - } - - // Look at the next block - we might have a loose list. Or an extra - // paragraph for the current li - var contained = get_contained_blocks( stack.length, next ); - - // Deal with code blocks or properly nested lists - if ( contained.length > 0 ) { - // Make sure all listitems up the stack are paragraphs - forEach( stack, paragraphify, this); - - last_li.push.apply( last_li, this.toTree( contained, [] ) ); - } - - var next_block = next[0] && next[0].valueOf() || ""; - - if ( next_block.match(is_list_re) || next_block.match( /^ / ) ) { - block = next.shift(); - - // Check for an HR following a list: features/lists/hr_abutting - var hr = this.dialect.block.horizRule( block, next ); - - if ( hr ) { - ret.push.apply(ret, hr); - break; - } - - // Make sure all listitems up the stack are paragraphs - forEach( stack, paragraphify, this); - - loose = true; - continue loose_search; - } - break; - } // loose_search - - return ret; - }; - })(), - - blockquote: function blockquote( block, next ) { - if ( !block.match( /^>/m ) ) - return undefined; - - var jsonml = []; - - // separate out the leading abutting block, if any. I.e. in this case: - // - // a - // > b - // - if ( block[ 0 ] != ">" ) { - var lines = block.split( /\n/ ), - prev = [], - line_no = block.lineNumber; - - // keep shifting lines until you find a crotchet - while ( lines.length && lines[ 0 ][ 0 ] != ">" ) { - prev.push( lines.shift() ); - line_no++; - } - - var abutting = mk_block( prev.join( "\n" ), "\n", block.lineNumber ); - jsonml.push.apply( jsonml, this.processBlock( abutting, [] ) ); - // reassemble new block of just block quotes! - block = mk_block( lines.join( "\n" ), block.trailing, line_no ); - } - - - // if the next block is also a blockquote merge it in - while ( next.length && next[ 0 ][ 0 ] == ">" ) { - var b = next.shift(); - block = mk_block( block + block.trailing + b, b.trailing, block.lineNumber ); - } - - // Strip off the leading "> " and re-process as a block. - var input = block.replace( /^> ?/gm, "" ), - old_tree = this.tree, - processedBlock = this.toTree( input, [ "blockquote" ] ), - attr = extract_attr( processedBlock ); - - // If any link references were found get rid of them - if ( attr && attr.references ) { - delete attr.references; - // And then remove the attribute object if it's empty - if ( isEmpty( attr ) ) { - processedBlock.splice( 1, 1 ); - } - } - - jsonml.push( processedBlock ); - return jsonml; - }, - - referenceDefn: function referenceDefn( block, next) { - var re = /^\s*\[(.*?)\]:\s*(\S+)(?:\s+(?:(['"])(.*?)\3|\((.*?)\)))?\n?/; - // interesting matches are [ , ref_id, url, , title, title ] - - if ( !block.match(re) ) - return undefined; - - // make an attribute node if it doesn't exist - if ( !extract_attr( this.tree ) ) { - this.tree.splice( 1, 0, {} ); - } - - var attrs = extract_attr( this.tree ); - - // make a references hash if it doesn't exist - if ( attrs.references === undefined ) { - attrs.references = {}; - } - - var b = this.loop_re_over_block(re, block, function( m ) { - - if ( m[2] && m[2][0] == "<" && m[2][m[2].length-1] == ">" ) - m[2] = m[2].substring( 1, m[2].length - 1 ); - - var ref = attrs.references[ m[1].toLowerCase() ] = { - href: m[2] - }; - - if ( m[4] !== undefined ) - ref.title = m[4]; - else if ( m[5] !== undefined ) - ref.title = m[5]; - - } ); - - if ( b.length ) - next.unshift( mk_block( b, block.trailing ) ); - - return []; - }, - - para: function para( block, next ) { - // everything's a para! - return [ ["para"].concat( this.processInline( block ) ) ]; - } - } -}; - -Markdown.dialects.Gruber.inline = { - - __oneElement__: function oneElement( text, patterns_or_re, previous_nodes ) { - var m, - res, - lastIndex = 0; - - patterns_or_re = patterns_or_re || this.dialect.inline.__patterns__; - var re = new RegExp( "([\\s\\S]*?)(" + (patterns_or_re.source || patterns_or_re) + ")" ); - - m = re.exec( text ); - if (!m) { - // Just boring text - return [ text.length, text ]; - } - else if ( m[1] ) { - // Some un-interesting text matched. Return that first - return [ m[1].length, m[1] ]; - } - - var res; - if ( m[2] in this.dialect.inline ) { - res = this.dialect.inline[ m[2] ].call( - this, - text.substr( m.index ), m, previous_nodes || [] ); - } - // Default for now to make dev easier. just slurp special and output it. - res = res || [ m[2].length, m[2] ]; - return res; - }, - - __call__: function inline( text, patterns ) { - - var out = [], - res; - - function add(x) { - //D:self.debug(" adding output", uneval(x)); - if ( typeof x == "string" && typeof out[out.length-1] == "string" ) - out[ out.length-1 ] += x; - else - out.push(x); - } - - while ( text.length > 0 ) { - res = this.dialect.inline.__oneElement__.call(this, text, patterns, out ); - text = text.substr( res.shift() ); - forEach(res, add ) - } - - return out; - }, - - // These characters are intersting elsewhere, so have rules for them so that - // chunks of plain text blocks don't include them - "]": function () {}, - "}": function () {}, - - __escape__ : /^\\[\\`\*_{}\[\]()#\+.!\-]/, - - "\\": function escaped( text ) { - // [ length of input processed, node/children to add... ] - // Only esacape: \ ` * _ { } [ ] ( ) # * + - . ! - if ( this.dialect.inline.__escape__.exec( text ) ) - return [ 2, text.charAt( 1 ) ]; - else - // Not an esacpe - return [ 1, "\\" ]; - }, - - "![": function image( text ) { - - // Unlike images, alt text is plain text only. no other elements are - // allowed in there - - // ![Alt text](/path/to/img.jpg "Optional title") - // 1 2 3 4 <--- captures - var m = text.match( /^!\[(.*?)\][ \t]*\([ \t]*([^")]*?)(?:[ \t]+(["'])(.*?)\3)?[ \t]*\)/ ); - - if ( m ) { - if ( m[2] && m[2][0] == "<" && m[2][m[2].length-1] == ">" ) - m[2] = m[2].substring( 1, m[2].length - 1 ); - - m[2] = this.dialect.inline.__call__.call( this, m[2], /\\/ )[0]; - - var attrs = { alt: m[1], href: m[2] || "" }; - if ( m[4] !== undefined) - attrs.title = m[4]; - - return [ m[0].length, [ "img", attrs ] ]; - } - - // ![Alt text][id] - m = text.match( /^!\[(.*?)\][ \t]*\[(.*?)\]/ ); - - if ( m ) { - // We can't check if the reference is known here as it likely wont be - // found till after. Check it in md tree->hmtl tree conversion - return [ m[0].length, [ "img_ref", { alt: m[1], ref: m[2].toLowerCase(), original: m[0] } ] ]; - } - - // Just consume the '![' - return [ 2, "![" ]; - }, - - "[": function link( text ) { - - var orig = String(text); - // Inline content is possible inside `link text` - var res = Markdown.DialectHelpers.inline_until_char.call( this, text.substr(1), "]" ); - - // No closing ']' found. Just consume the [ - if ( !res ) return [ 1, "[" ]; - - var consumed = 1 + res[ 0 ], - children = res[ 1 ], - link, - attrs; - - // At this point the first [...] has been parsed. See what follows to find - // out which kind of link we are (reference or direct url) - text = text.substr( consumed ); - - // [link text](/path/to/img.jpg "Optional title") - // 1 2 3 <--- captures - // This will capture up to the last paren in the block. We then pull - // back based on if there a matching ones in the url - // ([here](/url/(test)) - // The parens have to be balanced - var m = text.match( /^\s*\([ \t]*([^"']*)(?:[ \t]+(["'])(.*?)\2)?[ \t]*\)/ ); - if ( m ) { - var url = m[1]; - consumed += m[0].length; - - if ( url && url[0] == "<" && url[url.length-1] == ">" ) - url = url.substring( 1, url.length - 1 ); - - // If there is a title we don't have to worry about parens in the url - if ( !m[3] ) { - var open_parens = 1; // One open that isn't in the capture - for ( var len = 0; len < url.length; len++ ) { - switch ( url[len] ) { - case "(": - open_parens++; - break; - case ")": - if ( --open_parens == 0) { - consumed -= url.length - len; - url = url.substring(0, len); - } - break; - } - } - } - - // Process escapes only - url = this.dialect.inline.__call__.call( this, url, /\\/ )[0]; - - attrs = { href: url || "" }; - if ( m[3] !== undefined) - attrs.title = m[3]; - - link = [ "link", attrs ].concat( children ); - return [ consumed, link ]; - } - - // [Alt text][id] - // [Alt text] [id] - m = text.match( /^\s*\[(.*?)\]/ ); - - if ( m ) { - - consumed += m[ 0 ].length; - - // [links][] uses links as its reference - attrs = { ref: ( m[ 1 ] || String(children) ).toLowerCase(), original: orig.substr( 0, consumed ) }; - - link = [ "link_ref", attrs ].concat( children ); - - // We can't check if the reference is known here as it likely wont be - // found till after. Check it in md tree->hmtl tree conversion. - // Store the original so that conversion can revert if the ref isn't found. - return [ consumed, link ]; - } - - // [id] - // Only if id is plain (no formatting.) - if ( children.length == 1 && typeof children[0] == "string" ) { - - attrs = { ref: children[0].toLowerCase(), original: orig.substr( 0, consumed ) }; - link = [ "link_ref", attrs, children[0] ]; - return [ consumed, link ]; - } - - // Just consume the "[" - return [ 1, "[" ]; - }, - - - "<": function autoLink( text ) { - var m; - - if ( ( m = text.match( /^<(?:((https?|ftp|mailto):[^>]+)|(.*?@.*?\.[a-zA-Z]+))>/ ) ) != null ) { - if ( m[3] ) { - return [ m[0].length, [ "link", { href: "mailto:" + m[3] }, m[3] ] ]; - - } - else if ( m[2] == "mailto" ) { - return [ m[0].length, [ "link", { href: m[1] }, m[1].substr("mailto:".length ) ] ]; - } - else - return [ m[0].length, [ "link", { href: m[1] }, m[1] ] ]; - } - - return [ 1, "<" ]; - }, - - "`": function inlineCode( text ) { - // Inline code block. as many backticks as you like to start it - // Always skip over the opening ticks. - var m = text.match( /(`+)(([\s\S]*?)\1)/ ); - - if ( m && m[2] ) - return [ m[1].length + m[2].length, [ "inlinecode", m[3] ] ]; - else { - // TODO: No matching end code found - warn! - return [ 1, "`" ]; - } - }, - - " \n": function lineBreak( text ) { - return [ 3, [ "linebreak" ] ]; + Markdown.prototype.debug = function () { + var args = Array.prototype.slice.call(arguments); + args.unshift(this.debug_indent); + if (typeof print !== "undefined") + print.apply(print, args); + if (typeof console !== "undefined" && typeof console.log !== "undefined") + console.log.apply(null, args); } -}; + Markdown.prototype.loop_re_over_block = function (re, block, cb) { + // Dont use /g regexps with this + var m, + b = block.valueOf(); + + while (b.length && (m = re.exec(b)) != null) { + b = b.substr(m[0].length); + cb.call(this, m); + } + return b; + }; + + /** + * Markdown.dialects + * + * Namespace of built-in dialects. + **/ + Markdown.dialects = {}; + + /** + * Markdown.dialects.Gruber + * + * The default dialect that follows the rules set out by John Gruber's + * markdown.pl as closely as possible. Well actually we follow the behaviour of + * that script which in some places is not exactly what the syntax web page + * says. + **/ + Markdown.dialects.Gruber = { + block: { + atxHeader: function atxHeader(block, next) { + var m = block.match(/^(#{1,6})\s*(.*?)\s*#*\s*(?:\n|$)/); + + if (!m) return undefined; + + var header = ["header", {level: m[1].length}]; + Array.prototype.push.apply(header, this.processInline(m[2])); + + if (m[0].length < block.length) + next.unshift(mk_block(block.substr(m[0].length), block.trailing, block.lineNumber + 2)); + + return [header]; + }, + + setextHeader: function setextHeader(block, next) { + var m = block.match(/^(.*)\n([-=])\2\2+(?:\n|$)/); + + if (!m) return undefined; + + var level = (m[2] === "=") ? 1 : 2; + var header = ["header", {level: level}, m[1]]; + + if (m[0].length < block.length) + next.unshift(mk_block(block.substr(m[0].length), block.trailing, block.lineNumber + 2)); + + return [header]; + }, + + code: function code(block, next) { + // | Foo + // |bar + // should be a code block followed by a paragraph. Fun + // + // There might also be adjacent code block to merge. + + var ret = [], + re = /^(?: {0,3}\t| {4})(.*)\n?/, + lines; + + // 4 spaces + content + if (!block.match(re)) return undefined; + + block_search: + do { + // Now pull out the rest of the lines + var b = this.loop_re_over_block( + re, block.valueOf(), function (m) { + ret.push(m[1]); + }); + + if (b.length) { + // Case alluded to in first comment. push it back on as a new block + next.unshift(mk_block(b, block.trailing)); + break block_search; + } else if (next.length) { + // Check the next block - it might be code too + if (!next[0].match(re)) break block_search; + + // Pull how how many blanks lines follow - minus two to account for .join + ret.push(block.trailing.replace(/[^\n]/g, "").substring(2)); + + block = next.shift(); + } else { + break block_search; + } + } while (true); + + return [["code_block", ret.join("\n")]]; + }, + + horizRule: function horizRule(block, next) { + // this needs to find any hr in the block to handle abutting blocks + var m = block.match(/^(?:([\s\S]*?)\n)?[ \t]*([-_*])(?:[ \t]*\2){2,}[ \t]*(?:\n([\s\S]*))?$/); + + if (!m) { + return undefined; + } + + var jsonml = [["hr"]]; + + // if there's a leading abutting block, process it + if (m[1]) { + jsonml.unshift.apply(jsonml, this.processBlock(m[1], [])); + } + + // if there's a trailing abutting block, stick it into next + if (m[3]) { + next.unshift(mk_block(m[3])); + } + + return jsonml; + }, + + // There are two types of lists. Tight and loose. Tight lists have no whitespace + // between the items (and result in text just in the
  • ) and loose lists, + // which have an empty line between list items, resulting in (one or more) + // paragraphs inside the
  • . + // + // There are all sorts weird edge cases about the original markdown.pl's + // handling of lists: + // + // * Nested lists are supposed to be indented by four chars per level. But + // if they aren't, you can get a nested list by indenting by less than + // four so long as the indent doesn't match an indent of an existing list + // item in the 'nest stack'. + // + // * The type of the list (bullet or number) is controlled just by the + // first item at the indent. Subsequent changes are ignored unless they + // are for nested lists + // + lists: (function () { + // Use a closure to hide a few variables. + var any_list = "[*+-]|\\d+\\.", + bullet_list = /[*+-]/, + number_list = /\d+\./, + // Capture leading indent as it matters for determining nested lists. + is_list_re = new RegExp("^( {0,3})(" + any_list + ")[ \t]+"), + indent_re = "(?: {0,3}\\t| {4})"; + + // TODO: Cache this regexp for certain depths. + // Create a regexp suitable for matching an li for a given stack depth + function regex_for_depth(depth) { + + return new RegExp( + // m[1] = indent, m[2] = list_type + "(?:^(" + indent_re + "{0," + depth + "} {0,3})(" + any_list + ")\\s+)|" + + // m[3] = cont + "(^" + indent_re + "{0," + (depth - 1) + "}[ ]{0,4})" + ); + } + + function expand_tab(input) { + return input.replace(/ {0,3}\t/g, " "); + } + + // Add inline content `inline` to `li`. inline comes from processInline + // so is an array of content + function add(li, loose, inline, nl) { + if (loose) { + li.push(["para"].concat(inline)); + return; + } + // Hmmm, should this be any block level element or just paras? + var add_to = li[li.length - 1] instanceof Array && li[li.length - 1][0] == "para" + ? li[li.length - 1] + : li; + + // If there is already some content in this list, add the new line in + if (nl && li.length > 1) inline.unshift(nl); + + for (var i = 0; i < inline.length; i++) { + var what = inline[i], + is_str = typeof what == "string"; + if (is_str && add_to.length > 1 && typeof add_to[add_to.length - 1] == "string") { + add_to[add_to.length - 1] += what; + } else { + add_to.push(what); + } + } + } + + // contained means have an indent greater than the current one. On + // *every* line in the block + function get_contained_blocks(depth, blocks) { + + var re = new RegExp("^(" + indent_re + "{" + depth + "}.*?\\n?)*$"), + replace = new RegExp("^" + indent_re + "{" + depth + "}", "gm"), + ret = []; + + while (blocks.length > 0) { + if (re.exec(blocks[0])) { + var b = blocks.shift(), + // Now remove that indent + x = b.replace(replace, ""); + + ret.push(mk_block(x, b.trailing, b.lineNumber)); + } else { + break; + } + } + return ret; + } + + // passed to stack.forEach to turn list items up the stack into paras + function paragraphify(s, i, stack) { + var list = s.list; + var last_li = list[list.length - 1]; + + if (last_li[1] instanceof Array && last_li[1][0] == "para") { + return; + } + if (i + 1 == stack.length) { + // Last stack frame + // Keep the same array, but replace the contents + last_li.push(["para"].concat(last_li.splice(1, last_li.length - 1))); + } else { + var sublist = last_li.pop(); + last_li.push(["para"].concat(last_li.splice(1, last_li.length - 1)), sublist); + } + } + + // The matcher function + return function (block, next) { + var m = block.match(is_list_re); + if (!m) return undefined; + + function make_list(m) { + var list = bullet_list.exec(m[2]) + ? ["bulletlist"] + : ["numberlist"]; + + stack.push({list: list, indent: m[1]}); + return list; + } + + + var stack = [], // Stack of lists for nesting. + list = make_list(m), + last_li, + loose = false, + ret = [stack[0].list], + i; + + // Loop to search over block looking for inner block elements and loose lists + loose_search: + while (true) { + // Split into lines preserving new lines at end of line + var lines = block.split(/(?=\n)/); + + // We have to grab all lines for a li and call processInline on them + // once as there are some inline things that can span lines. + var li_accumulate = ""; + + // Loop over the lines in this block looking for tight lists. + tight_search: + for (var line_no = 0; line_no < lines.length; line_no++) { + var nl = "", + l = lines[line_no].replace(/^\n/, function (n) { + nl = n; + return ""; + }); + + // TODO: really should cache this + var line_re = regex_for_depth(stack.length); + + m = l.match(line_re); + //print( "line:", uneval(l), "\nline match:", uneval(m) ); + + // We have a list item + if (m[1] !== undefined) { + // Process the previous list item, if any + if (li_accumulate.length) { + add(last_li, loose, this.processInline(li_accumulate), nl); + // Loose mode will have been dealt with. Reset it + loose = false; + li_accumulate = ""; + } + + m[1] = expand_tab(m[1]); + var wanted_depth = Math.floor(m[1].length / 4) + 1; + //print( "want:", wanted_depth, "stack:", stack.length); + if (wanted_depth > stack.length) { + // Deep enough for a nested list outright + //print ( "new nested list" ); + list = make_list(m); + last_li.push(list); + last_li = list[1] = ["listitem"]; + } else { + // We aren't deep enough to be strictly a new level. This is + // where Md.pl goes nuts. If the indent matches a level in the + // stack, put it there, else put it one deeper then the + // wanted_depth deserves. + var found = false; + for (i = 0; i < stack.length; i++) { + if (stack[i].indent != m[1]) continue; + list = stack[i].list; + stack.splice(i + 1, stack.length - (i + 1)); + found = true; + break; + } + + if (!found) { + //print("not found. l:", uneval(l)); + wanted_depth++; + if (wanted_depth <= stack.length) { + stack.splice(wanted_depth, stack.length - wanted_depth); + //print("Desired depth now", wanted_depth, "stack:", stack.length); + list = stack[wanted_depth - 1].list; + //print("list:", uneval(list) ); + } else { + //print ("made new stack for messy indent"); + list = make_list(m); + last_li.push(list); + } + } + + //print( uneval(list), "last", list === stack[stack.length-1].list ); + last_li = ["listitem"]; + list.push(last_li); + } // end depth of shenegains + nl = ""; + } + + // Add content + if (l.length > m[0].length) { + li_accumulate += nl + l.substr(m[0].length); + } + } // tight_search + + if (li_accumulate.length) { + add(last_li, loose, this.processInline(li_accumulate), nl); + // Loose mode will have been dealt with. Reset it + loose = false; + li_accumulate = ""; + } + + // Look at the next block - we might have a loose list. Or an extra + // paragraph for the current li + var contained = get_contained_blocks(stack.length, next); + + // Deal with code blocks or properly nested lists + if (contained.length > 0) { + // Make sure all listitems up the stack are paragraphs + forEach(stack, paragraphify, this); + + last_li.push.apply(last_li, this.toTree(contained, [])); + } + + var next_block = next[0] && next[0].valueOf() || ""; + + if (next_block.match(is_list_re) || next_block.match(/^ /)) { + block = next.shift(); + + // Check for an HR following a list: features/lists/hr_abutting + var hr = this.dialect.block.horizRule(block, next); + + if (hr) { + ret.push.apply(ret, hr); + break; + } + + // Make sure all listitems up the stack are paragraphs + forEach(stack, paragraphify, this); + + loose = true; + continue loose_search; + } + break; + } // loose_search + + return ret; + }; + })(), + + blockquote: function blockquote(block, next) { + if (!block.match(/^>/m)) + return undefined; + + var jsonml = []; + + // separate out the leading abutting block, if any. I.e. in this case: + // + // a + // > b + // + if (block[0] != ">") { + var lines = block.split(/\n/), + prev = [], + line_no = block.lineNumber; + + // keep shifting lines until you find a crotchet + while (lines.length && lines[0][0] != ">") { + prev.push(lines.shift()); + line_no++; + } + + var abutting = mk_block(prev.join("\n"), "\n", block.lineNumber); + jsonml.push.apply(jsonml, this.processBlock(abutting, [])); + // reassemble new block of just block quotes! + block = mk_block(lines.join("\n"), block.trailing, line_no); + } + + + // if the next block is also a blockquote merge it in + while (next.length && next[0][0] == ">") { + var b = next.shift(); + block = mk_block(block + block.trailing + b, b.trailing, block.lineNumber); + } + + // Strip off the leading "> " and re-process as a block. + var input = block.replace(/^> ?/gm, ""), + old_tree = this.tree, + processedBlock = this.toTree(input, ["blockquote"]), + attr = extract_attr(processedBlock); + + // If any link references were found get rid of them + if (attr && attr.references) { + delete attr.references; + // And then remove the attribute object if it's empty + if (isEmpty(attr)) { + processedBlock.splice(1, 1); + } + } + + jsonml.push(processedBlock); + return jsonml; + }, + + referenceDefn: function referenceDefn(block, next) { + var re = /^\s*\[(.*?)\]:\s*(\S+)(?:\s+(?:(['"])(.*?)\3|\((.*?)\)))?\n?/; + // interesting matches are [ , ref_id, url, , title, title ] + + if (!block.match(re)) + return undefined; + + // make an attribute node if it doesn't exist + if (!extract_attr(this.tree)) { + this.tree.splice(1, 0, {}); + } + + var attrs = extract_attr(this.tree); + + // make a references hash if it doesn't exist + if (attrs.references === undefined) { + attrs.references = {}; + } + + var b = this.loop_re_over_block(re, block, function (m) { + + if (m[2] && m[2][0] == "<" && m[2][m[2].length - 1] == ">") + m[2] = m[2].substring(1, m[2].length - 1); + + var ref = attrs.references[m[1].toLowerCase()] = { + href: m[2] + }; + + if (m[4] !== undefined) + ref.title = m[4]; + else if (m[5] !== undefined) + ref.title = m[5]; + + }); + + if (b.length) + next.unshift(mk_block(b, block.trailing)); + + return []; + }, + + para: function para(block, next) { + // everything's a para! + return [["para"].concat(this.processInline(block))]; + } + } + }; + + Markdown.dialects.Gruber.inline = { + + __oneElement__: function oneElement(text, patterns_or_re, previous_nodes) { + var m, + res, + lastIndex = 0; + + patterns_or_re = patterns_or_re || this.dialect.inline.__patterns__; + var re = new RegExp("([\\s\\S]*?)(" + (patterns_or_re.source || patterns_or_re) + ")"); + + m = re.exec(text); + if (!m) { + // Just boring text + return [text.length, text]; + } else if (m[1]) { + // Some un-interesting text matched. Return that first + return [m[1].length, m[1]]; + } + + var res; + if (m[2] in this.dialect.inline) { + res = this.dialect.inline[m[2]].call( + this, + text.substr(m.index), m, previous_nodes || []); + } + // Default for now to make dev easier. just slurp special and output it. + res = res || [m[2].length, m[2]]; + return res; + }, + + __call__: function inline(text, patterns) { + + var out = [], + res; + + function add(x) { + //D:self.debug(" adding output", uneval(x)); + if (typeof x == "string" && typeof out[out.length - 1] == "string") + out[out.length - 1] += x; + else + out.push(x); + } + + while (text.length > 0) { + res = this.dialect.inline.__oneElement__.call(this, text, patterns, out); + text = text.substr(res.shift()); + forEach(res, add) + } + + return out; + }, + + // These characters are intersting elsewhere, so have rules for them so that + // chunks of plain text blocks don't include them + "]": function () { + }, + "}": function () { + }, + + __escape__: /^\\[\\`\*_{}\[\]()#\+.!\-]/, + + "\\": function escaped(text) { + // [ length of input processed, node/children to add... ] + // Only esacape: \ ` * _ { } [ ] ( ) # * + - . ! + if (this.dialect.inline.__escape__.exec(text)) + return [2, text.charAt(1)]; + else + // Not an esacpe + return [1, "\\"]; + }, + + "![": function image(text) { + + // Unlike images, alt text is plain text only. no other elements are + // allowed in there + + // ![Alt text](/path/to/img.jpg "Optional title") + // 1 2 3 4 <--- captures + var m = text.match(/^!\[(.*?)\][ \t]*\([ \t]*([^")]*?)(?:[ \t]+(["'])(.*?)\3)?[ \t]*\)/); + + if (m) { + if (m[2] && m[2][0] == "<" && m[2][m[2].length - 1] == ">") + m[2] = m[2].substring(1, m[2].length - 1); + + m[2] = this.dialect.inline.__call__.call(this, m[2], /\\/)[0]; + + var attrs = {alt: m[1], href: m[2] || ""}; + if (m[4] !== undefined) + attrs.title = m[4]; + + return [m[0].length, ["img", attrs]]; + } + + // ![Alt text][id] + m = text.match(/^!\[(.*?)\][ \t]*\[(.*?)\]/); + + if (m) { + // We can't check if the reference is known here as it likely wont be + // found till after. Check it in md tree->hmtl tree conversion + return [m[0].length, ["img_ref", {alt: m[1], ref: m[2].toLowerCase(), original: m[0]}]]; + } + + // Just consume the '![' + return [2, "!["]; + }, + + "[": function link(text) { + + var orig = String(text); + // Inline content is possible inside `link text` + var res = Markdown.DialectHelpers.inline_until_char.call(this, text.substr(1), "]"); + + // No closing ']' found. Just consume the [ + if (!res) return [1, "["]; + + var consumed = 1 + res[0], + children = res[1], + link, + attrs; + + // At this point the first [...] has been parsed. See what follows to find + // out which kind of link we are (reference or direct url) + text = text.substr(consumed); + + // [link text](/path/to/img.jpg "Optional title") + // 1 2 3 <--- captures + // This will capture up to the last paren in the block. We then pull + // back based on if there a matching ones in the url + // ([here](/url/(test)) + // The parens have to be balanced + var m = text.match(/^\s*\([ \t]*([^"']*)(?:[ \t]+(["'])(.*?)\2)?[ \t]*\)/); + if (m) { + var url = m[1]; + consumed += m[0].length; + + if (url && url[0] == "<" && url[url.length - 1] == ">") + url = url.substring(1, url.length - 1); + + // If there is a title we don't have to worry about parens in the url + if (!m[3]) { + var open_parens = 1; // One open that isn't in the capture + for (var len = 0; len < url.length; len++) { + switch (url[len]) { + case "(": + open_parens++; + break; + case ")": + if (--open_parens == 0) { + consumed -= url.length - len; + url = url.substring(0, len); + } + break; + } + } + } + + // Process escapes only + url = this.dialect.inline.__call__.call(this, url, /\\/)[0]; + + attrs = {href: url || ""}; + if (m[3] !== undefined) + attrs.title = m[3]; + + link = ["link", attrs].concat(children); + return [consumed, link]; + } + + // [Alt text][id] + // [Alt text] [id] + m = text.match(/^\s*\[(.*?)\]/); + + if (m) { + + consumed += m[0].length; + + // [links][] uses links as its reference + attrs = {ref: (m[1] || String(children)).toLowerCase(), original: orig.substr(0, consumed)}; + + link = ["link_ref", attrs].concat(children); + + // We can't check if the reference is known here as it likely wont be + // found till after. Check it in md tree->hmtl tree conversion. + // Store the original so that conversion can revert if the ref isn't found. + return [consumed, link]; + } + + // [id] + // Only if id is plain (no formatting.) + if (children.length == 1 && typeof children[0] == "string") { + + attrs = {ref: children[0].toLowerCase(), original: orig.substr(0, consumed)}; + link = ["link_ref", attrs, children[0]]; + return [consumed, link]; + } + + // Just consume the "[" + return [1, "["]; + }, + + + "<": function autoLink(text) { + var m; + + if ((m = text.match(/^<(?:((https?|ftp|mailto):[^>]+)|(.*?@.*?\.[a-zA-Z]+))>/)) != null) { + if (m[3]) { + return [m[0].length, ["link", {href: "mailto:" + m[3]}, m[3]]]; + + } else if (m[2] == "mailto") { + return [m[0].length, ["link", {href: m[1]}, m[1].substr("mailto:".length)]]; + } else + return [m[0].length, ["link", {href: m[1]}, m[1]]]; + } + + return [1, "<"]; + }, + + "`": function inlineCode(text) { + // Inline code block. as many backticks as you like to start it + // Always skip over the opening ticks. + var m = text.match(/(`+)(([\s\S]*?)\1)/); + + if (m && m[2]) + return [m[1].length + m[2].length, ["inlinecode", m[3]]]; + else { + // TODO: No matching end code found - warn! + return [1, "`"]; + } + }, + + " \n": function lineBreak(text) { + return [3, ["linebreak"]]; + } + + }; // Meta Helper/generator method for em and strong handling -function strong_em( tag, md ) { + function strong_em(tag, md) { - var state_slot = tag + "_state", - other_slot = tag == "strong" ? "em_state" : "strong_state"; + var state_slot = tag + "_state", + other_slot = tag == "strong" ? "em_state" : "strong_state"; - function CloseTag(len) { - this.len_after = len; - this.name = "close_" + md; - } + function CloseTag(len) { + this.len_after = len; + this.name = "close_" + md; + } - return function ( text, orig_match ) { + return function (text, orig_match) { - if ( this[state_slot][0] == md ) { - // Most recent em is of this type - //D:this.debug("closing", md); - this[state_slot].shift(); + if (this[state_slot][0] == md) { + // Most recent em is of this type + //D:this.debug("closing", md); + this[state_slot].shift(); - // "Consume" everything to go back to the recrusion in the else-block below - return[ text.length, new CloseTag(text.length-md.length) ]; + // "Consume" everything to go back to the recrusion in the else-block below + return [text.length, new CloseTag(text.length - md.length)]; + } else { + // Store a clone of the em/strong states + var other = this[other_slot].slice(), + state = this[state_slot].slice(); + + this[state_slot].unshift(md); + + //D:this.debug_indent += " "; + + // Recurse + var res = this.processInline(text.substr(md.length)); + //D:this.debug_indent = this.debug_indent.substr(2); + + var last = res[res.length - 1]; + + //D:this.debug("processInline from", tag + ": ", uneval( res ) ); + + var check = this[state_slot].shift(); + if (last instanceof CloseTag) { + res.pop(); + // We matched! Huzzah. + var consumed = text.length - last.len_after; + return [consumed, [tag].concat(res)]; + } else { + // Restore the state of the other kind. We might have mistakenly closed it. + this[other_slot] = other; + this[state_slot] = state; + + // We can't reuse the processed result as it could have wrong parsing contexts in it. + return [md.length, md]; + } + } + }; // End returned function } - else { - // Store a clone of the em/strong states - var other = this[other_slot].slice(), - state = this[state_slot].slice(); - this[state_slot].unshift(md); - - //D:this.debug_indent += " "; - - // Recurse - var res = this.processInline( text.substr( md.length ) ); - //D:this.debug_indent = this.debug_indent.substr(2); - - var last = res[res.length - 1]; - - //D:this.debug("processInline from", tag + ": ", uneval( res ) ); - - var check = this[state_slot].shift(); - if ( last instanceof CloseTag ) { - res.pop(); - // We matched! Huzzah. - var consumed = text.length - last.len_after; - return [ consumed, [ tag ].concat(res) ]; - } - else { - // Restore the state of the other kind. We might have mistakenly closed it. - this[other_slot] = other; - this[state_slot] = state; - - // We can't reuse the processed result as it could have wrong parsing contexts in it. - return [ md.length, md ]; - } - } - }; // End returned function -} - -Markdown.dialects.Gruber.inline["**"] = strong_em("strong", "**"); -Markdown.dialects.Gruber.inline["__"] = strong_em("strong", "__"); -Markdown.dialects.Gruber.inline["*"] = strong_em("em", "*"); -Markdown.dialects.Gruber.inline["_"] = strong_em("em", "_"); + Markdown.dialects.Gruber.inline["**"] = strong_em("strong", "**"); + Markdown.dialects.Gruber.inline["__"] = strong_em("strong", "__"); + Markdown.dialects.Gruber.inline["*"] = strong_em("em", "*"); + Markdown.dialects.Gruber.inline["_"] = strong_em("em", "_"); // Build default order from insertion order. -Markdown.buildBlockOrder = function(d) { - var ord = []; - for ( var i in d ) { - if ( i == "__order__" || i == "__call__" ) continue; - ord.push( i ); - } - d.__order__ = ord; -}; + Markdown.buildBlockOrder = function (d) { + var ord = []; + for (var i in d) { + if (i == "__order__" || i == "__call__") continue; + ord.push(i); + } + d.__order__ = ord; + }; // Build patterns for inline matcher -Markdown.buildInlinePatterns = function(d) { - var patterns = []; + Markdown.buildInlinePatterns = function (d) { + var patterns = []; - for ( var i in d ) { - // __foo__ is reserved and not a pattern - if ( i.match( /^__.*__$/) ) continue; - var l = i.replace( /([\\.*+?|()\[\]{}])/g, "\\$1" ) - .replace( /\n/, "\\n" ); - patterns.push( i.length == 1 ? l : "(?:" + l + ")" ); - } + for (var i in d) { + // __foo__ is reserved and not a pattern + if (i.match(/^__.*__$/)) continue; + var l = i.replace(/([\\.*+?|()\[\]{}])/g, "\\$1") + .replace(/\n/, "\\n"); + patterns.push(i.length == 1 ? l : "(?:" + l + ")"); + } - patterns = patterns.join("|"); - d.__patterns__ = patterns; - //print("patterns:", uneval( patterns ) ); + patterns = patterns.join("|"); + d.__patterns__ = patterns; + //print("patterns:", uneval( patterns ) ); - var fn = d.__call__; - d.__call__ = function(text, pattern) { - if ( pattern != undefined ) { - return fn.call(this, text, pattern); + var fn = d.__call__; + d.__call__ = function (text, pattern) { + if (pattern != undefined) { + return fn.call(this, text, pattern); + } else { + return fn.call(this, text, patterns); + } + }; + }; + + Markdown.DialectHelpers = {}; + Markdown.DialectHelpers.inline_until_char = function (text, want) { + var consumed = 0, + nodes = []; + + while (true) { + if (text.charAt(consumed) == want) { + // Found the character we were looking for + consumed++; + return [consumed, nodes]; + } + + if (consumed >= text.length) { + // No closing char found. Abort. + return null; + } + + var res = this.dialect.inline.__oneElement__.call(this, text.substr(consumed)); + consumed += res[0]; + // Add any returned nodes. + nodes.push.apply(nodes, res.slice(1)); + } } - else - { - return fn.call(this, text, patterns); - } - }; -}; - -Markdown.DialectHelpers = {}; -Markdown.DialectHelpers.inline_until_char = function( text, want ) { - var consumed = 0, - nodes = []; - - while ( true ) { - if ( text.charAt( consumed ) == want ) { - // Found the character we were looking for - consumed++; - return [ consumed, nodes ]; - } - - if ( consumed >= text.length ) { - // No closing char found. Abort. - return null; - } - - var res = this.dialect.inline.__oneElement__.call(this, text.substr( consumed ) ); - consumed += res[ 0 ]; - // Add any returned nodes. - nodes.push.apply( nodes, res.slice( 1 ) ); - } -} // Helper function to make sub-classing a dialect easier -Markdown.subclassDialect = function( d ) { - function Block() {} - Block.prototype = d.block; - function Inline() {} - Inline.prototype = d.inline; - - return { block: new Block(), inline: new Inline() }; -}; - -Markdown.buildBlockOrder ( Markdown.dialects.Gruber.block ); -Markdown.buildInlinePatterns( Markdown.dialects.Gruber.inline ); - -Markdown.dialects.Maruku = Markdown.subclassDialect( Markdown.dialects.Gruber ); - -Markdown.dialects.Maruku.processMetaHash = function processMetaHash( meta_string ) { - var meta = split_meta_hash( meta_string ), - attr = {}; - - for ( var i = 0; i < meta.length; ++i ) { - // id: #foo - if ( /^#/.test( meta[ i ] ) ) { - attr.id = meta[ i ].substring( 1 ); - } - // class: .foo - else if ( /^\./.test( meta[ i ] ) ) { - // if class already exists, append the new one - if ( attr["class"] ) { - attr["class"] = attr["class"] + meta[ i ].replace( /./, " " ); - } - else { - attr["class"] = meta[ i ].substring( 1 ); - } - } - // attribute: foo=bar - else if ( /\=/.test( meta[ i ] ) ) { - var s = meta[ i ].split( /\=/ ); - attr[ s[ 0 ] ] = s[ 1 ]; - } - } - - return attr; -} - -function split_meta_hash( meta_string ) { - var meta = meta_string.split( "" ), - parts = [ "" ], - in_quotes = false; - - while ( meta.length ) { - var letter = meta.shift(); - switch ( letter ) { - case " " : - // if we're in a quoted section, keep it - if ( in_quotes ) { - parts[ parts.length - 1 ] += letter; + Markdown.subclassDialect = function (d) { + function Block() { } - // otherwise make a new part - else { - parts.push( "" ); + + Block.prototype = d.block; + + function Inline() { } - break; - case "'" : - case '"' : - // reverse the quotes and move straight on - in_quotes = !in_quotes; - break; - case "\\" : - // shift off the next letter to be used straight away. - // it was escaped so we'll keep it whatever it is - letter = meta.shift(); - default : - parts[ parts.length - 1 ] += letter; - break; - } - } - return parts; -} + Inline.prototype = d.inline; -Markdown.dialects.Maruku.block.document_meta = function document_meta( block, next ) { - // we're only interested in the first block - if ( block.lineNumber > 1 ) return undefined; + return {block: new Block(), inline: new Inline()}; + }; - // document_meta blocks consist of one or more lines of `Key: Value\n` - if ( ! block.match( /^(?:\w+:.*\n)*\w+:.*$/ ) ) return undefined; + Markdown.buildBlockOrder(Markdown.dialects.Gruber.block); + Markdown.buildInlinePatterns(Markdown.dialects.Gruber.inline); - // make an attribute node if it doesn't exist - if ( !extract_attr( this.tree ) ) { - this.tree.splice( 1, 0, {} ); - } + Markdown.dialects.Maruku = Markdown.subclassDialect(Markdown.dialects.Gruber); - var pairs = block.split( /\n/ ); - for ( p in pairs ) { - var m = pairs[ p ].match( /(\w+):\s*(.*)$/ ), - key = m[ 1 ].toLowerCase(), - value = m[ 2 ]; + Markdown.dialects.Maruku.processMetaHash = function processMetaHash(meta_string) { + var meta = split_meta_hash(meta_string), + attr = {}; - this.tree[ 1 ][ key ] = value; - } + for (var i = 0; i < meta.length; ++i) { + // id: #foo + if (/^#/.test(meta[i])) { + attr.id = meta[i].substring(1); + } + // class: .foo + else if (/^\./.test(meta[i])) { + // if class already exists, append the new one + if (attr["class"]) { + attr["class"] = attr["class"] + meta[i].replace(/./, " "); + } else { + attr["class"] = meta[i].substring(1); + } + } + // attribute: foo=bar + else if (/\=/.test(meta[i])) { + var s = meta[i].split(/\=/); + attr[s[0]] = s[1]; + } + } - // document_meta produces no content! - return []; -}; - -Markdown.dialects.Maruku.block.block_meta = function block_meta( block, next ) { - // check if the last line of the block is an meta hash - var m = block.match( /(^|\n) {0,3}\{:\s*((?:\\\}|[^\}])*)\s*\}$/ ); - if ( !m ) return undefined; - - // process the meta hash - var attr = this.dialect.processMetaHash( m[ 2 ] ); - - var hash; - - // if we matched ^ then we need to apply meta to the previous block - if ( m[ 1 ] === "" ) { - var node = this.tree[ this.tree.length - 1 ]; - hash = extract_attr( node ); - - // if the node is a string (rather than JsonML), bail - if ( typeof node === "string" ) return undefined; - - // create the attribute hash if it doesn't exist - if ( !hash ) { - hash = {}; - node.splice( 1, 0, hash ); + return attr; } - // add the attributes in - for ( a in attr ) { - hash[ a ] = attr[ a ]; + function split_meta_hash(meta_string) { + var meta = meta_string.split(""), + parts = [""], + in_quotes = false; + + while (meta.length) { + var letter = meta.shift(); + switch (letter) { + case " " : + // if we're in a quoted section, keep it + if (in_quotes) { + parts[parts.length - 1] += letter; + } + // otherwise make a new part + else { + parts.push(""); + } + break; + case "'" : + case '"' : + // reverse the quotes and move straight on + in_quotes = !in_quotes; + break; + case "\\" : + // shift off the next letter to be used straight away. + // it was escaped so we'll keep it whatever it is + letter = meta.shift(); + default : + parts[parts.length - 1] += letter; + break; + } + } + + return parts; } - // return nothing so the meta hash is removed - return []; - } + Markdown.dialects.Maruku.block.document_meta = function document_meta(block, next) { + // we're only interested in the first block + if (block.lineNumber > 1) return undefined; - // pull the meta hash off the block and process what's left - var b = block.replace( /\n.*$/, "" ), - result = this.processBlock( b, [] ); + // document_meta blocks consist of one or more lines of `Key: Value\n` + if (!block.match(/^(?:\w+:.*\n)*\w+:.*$/)) return undefined; - // get or make the attributes hash - hash = extract_attr( result[ 0 ] ); - if ( !hash ) { - hash = {}; - result[ 0 ].splice( 1, 0, hash ); - } + // make an attribute node if it doesn't exist + if (!extract_attr(this.tree)) { + this.tree.splice(1, 0, {}); + } - // attach the attributes to the block - for ( a in attr ) { - hash[ a ] = attr[ a ]; - } + var pairs = block.split(/\n/); + for (p in pairs) { + var m = pairs[p].match(/(\w+):\s*(.*)$/), + key = m[1].toLowerCase(), + value = m[2]; - return result; -}; + this.tree[1][key] = value; + } -Markdown.dialects.Maruku.block.definition_list = function definition_list( block, next ) { - // one or more terms followed by one or more definitions, in a single block - var tight = /^((?:[^\s:].*\n)+):\s+([\s\S]+)$/, - list = [ "dl" ], - i, m; + // document_meta produces no content! + return []; + }; - // see if we're dealing with a tight or loose block - if ( ( m = block.match( tight ) ) ) { - // pull subsequent tight DL blocks out of `next` - var blocks = [ block ]; - while ( next.length && tight.exec( next[ 0 ] ) ) { - blocks.push( next.shift() ); - } + Markdown.dialects.Maruku.block.block_meta = function block_meta(block, next) { + // check if the last line of the block is an meta hash + var m = block.match(/(^|\n) {0,3}\{:\s*((?:\\\}|[^\}])*)\s*\}$/); + if (!m) return undefined; - for ( var b = 0; b < blocks.length; ++b ) { - var m = blocks[ b ].match( tight ), - terms = m[ 1 ].replace( /\n$/, "" ).split( /\n/ ), - defns = m[ 2 ].split( /\n:\s+/ ); + // process the meta hash + var attr = this.dialect.processMetaHash(m[2]); - // print( uneval( m ) ); + var hash; - for ( i = 0; i < terms.length; ++i ) { - list.push( [ "dt", terms[ i ] ] ); - } + // if we matched ^ then we need to apply meta to the previous block + if (m[1] === "") { + var node = this.tree[this.tree.length - 1]; + hash = extract_attr(node); - for ( i = 0; i < defns.length; ++i ) { - // run inline processing over the definition - list.push( [ "dd" ].concat( this.processInline( defns[ i ].replace( /(\n)\s+/, "$1" ) ) ) ); - } - } - } - else { - return undefined; - } + // if the node is a string (rather than JsonML), bail + if (typeof node === "string") return undefined; - return [ list ]; -}; + // create the attribute hash if it doesn't exist + if (!hash) { + hash = {}; + node.splice(1, 0, hash); + } + + // add the attributes in + for (a in attr) { + hash[a] = attr[a]; + } + + // return nothing so the meta hash is removed + return []; + } + + // pull the meta hash off the block and process what's left + var b = block.replace(/\n.*$/, ""), + result = this.processBlock(b, []); + + // get or make the attributes hash + hash = extract_attr(result[0]); + if (!hash) { + hash = {}; + result[0].splice(1, 0, hash); + } + + // attach the attributes to the block + for (a in attr) { + hash[a] = attr[a]; + } + + return result; + }; + + Markdown.dialects.Maruku.block.definition_list = function definition_list(block, next) { + // one or more terms followed by one or more definitions, in a single block + var tight = /^((?:[^\s:].*\n)+):\s+([\s\S]+)$/, + list = ["dl"], + i, m; + + // see if we're dealing with a tight or loose block + if ((m = block.match(tight))) { + // pull subsequent tight DL blocks out of `next` + var blocks = [block]; + while (next.length && tight.exec(next[0])) { + blocks.push(next.shift()); + } + + for (var b = 0; b < blocks.length; ++b) { + var m = blocks[b].match(tight), + terms = m[1].replace(/\n$/, "").split(/\n/), + defns = m[2].split(/\n:\s+/); + + // print( uneval( m ) ); + + for (i = 0; i < terms.length; ++i) { + list.push(["dt", terms[i]]); + } + + for (i = 0; i < defns.length; ++i) { + // run inline processing over the definition + list.push(["dd"].concat(this.processInline(defns[i].replace(/(\n)\s+/, "$1")))); + } + } + } else { + return undefined; + } + + return [list]; + }; // splits on unescaped instances of @ch. If @ch is not a character the result // can be unpredictable -Markdown.dialects.Maruku.block.table = function table (block, next) { + Markdown.dialects.Maruku.block.table = function table(block, next) { - var _split_on_unescaped = function(s, ch) { - ch = ch || '\\s'; - if (ch.match(/^[\\|\[\]{}?*.+^$]$/)) { ch = '\\' + ch; } - var res = [ ], - r = new RegExp('^((?:\\\\.|[^\\\\' + ch + '])*)' + ch + '(.*)'), - m; - while(m = s.match(r)) { - res.push(m[1]); - s = m[2]; + var _split_on_unescaped = function (s, ch) { + ch = ch || '\\s'; + if (ch.match(/^[\\|\[\]{}?*.+^$]$/)) { + ch = '\\' + ch; + } + var res = [], + r = new RegExp('^((?:\\\\.|[^\\\\' + ch + '])*)' + ch + '(.*)'), + m; + while (m = s.match(r)) { + res.push(m[1]); + s = m[2]; + } + res.push(s); + return res; } - res.push(s); - return res; - } - var leading_pipe = /^ {0,3}\|(.+)\n {0,3}\|\s*([\-:]+[\-| :]*)\n((?:\s*\|.*(?:\n|$))*)(?=\n|$)/, - // find at least an unescaped pipe in each line - no_leading_pipe = /^ {0,3}(\S(?:\\.|[^\\|])*\|.*)\n {0,3}([\-:]+\s*\|[\-| :]*)\n((?:(?:\\.|[^\\|])*\|.*(?:\n|$))*)(?=\n|$)/, - i, m; - if (m = block.match(leading_pipe)) { - // remove leading pipes in contents - // (header and horizontal rule already have the leading pipe left out) - m[3] = m[3].replace(/^\s*\|/gm, ''); - } else if (! ( m = block.match(no_leading_pipe))) { - return undefined; - } - - var table = [ "table", [ "thead", [ "tr" ] ], [ "tbody" ] ]; - - // remove trailing pipes, then split on pipes - // (no escaped pipes are allowed in horizontal rule) - m[2] = m[2].replace(/\|\s*$/, '').split('|'); - - // process alignment - var html_attrs = [ ]; - forEach (m[2], function (s) { - if (s.match(/^\s*-+:\s*$/)) html_attrs.push({align: "right"}); - else if (s.match(/^\s*:-+\s*$/)) html_attrs.push({align: "left"}); - else if (s.match(/^\s*:-+:\s*$/)) html_attrs.push({align: "center"}); - else html_attrs.push({}); - }); - - // now for the header, avoid escaped pipes - m[1] = _split_on_unescaped(m[1].replace(/\|\s*$/, ''), '|'); - for (i = 0; i < m[1].length; i++) { - table[1][1].push(['th', html_attrs[i] || {}].concat( - this.processInline(m[1][i].trim()))); - } - - // now for body contents - forEach (m[3].replace(/\|\s*$/mg, '').split('\n'), function (row) { - var html_row = ['tr']; - row = _split_on_unescaped(row, '|'); - for (i = 0; i < row.length; i++) { - html_row.push(['td', html_attrs[i] || {}].concat(this.processInline(row[i].trim()))); + var leading_pipe = /^ {0,3}\|(.+)\n {0,3}\|\s*([\-:]+[\-| :]*)\n((?:\s*\|.*(?:\n|$))*)(?=\n|$)/, + // find at least an unescaped pipe in each line + no_leading_pipe = /^ {0,3}(\S(?:\\.|[^\\|])*\|.*)\n {0,3}([\-:]+\s*\|[\-| :]*)\n((?:(?:\\.|[^\\|])*\|.*(?:\n|$))*)(?=\n|$)/, + i, m; + if (m = block.match(leading_pipe)) { + // remove leading pipes in contents + // (header and horizontal rule already have the leading pipe left out) + m[3] = m[3].replace(/^\s*\|/gm, ''); + } else if (!(m = block.match(no_leading_pipe))) { + return undefined; } - table[2].push(html_row); - }, this); - return [table]; -} + var table = ["table", ["thead", ["tr"]], ["tbody"]]; -Markdown.dialects.Maruku.inline[ "{:" ] = function inline_meta( text, matches, out ) { - if ( !out.length ) { - return [ 2, "{:" ]; - } + // remove trailing pipes, then split on pipes + // (no escaped pipes are allowed in horizontal rule) + m[2] = m[2].replace(/\|\s*$/, '').split('|'); - // get the preceeding element - var before = out[ out.length - 1 ]; + // process alignment + var html_attrs = []; + forEach(m[2], function (s) { + if (s.match(/^\s*-+:\s*$/)) html_attrs.push({align: "right"}); + else if (s.match(/^\s*:-+\s*$/)) html_attrs.push({align: "left"}); + else if (s.match(/^\s*:-+:\s*$/)) html_attrs.push({align: "center"}); + else html_attrs.push({}); + }); - if ( typeof before === "string" ) { - return [ 2, "{:" ]; - } + // now for the header, avoid escaped pipes + m[1] = _split_on_unescaped(m[1].replace(/\|\s*$/, ''), '|'); + for (i = 0; i < m[1].length; i++) { + table[1][1].push(['th', html_attrs[i] || {}].concat( + this.processInline(m[1][i].trim()))); + } - // match a meta hash - var m = text.match( /^\{:\s*((?:\\\}|[^\}])*)\s*\}/ ); + // now for body contents + forEach(m[3].replace(/\|\s*$/mg, '').split('\n'), function (row) { + var html_row = ['tr']; + row = _split_on_unescaped(row, '|'); + for (i = 0; i < row.length; i++) { + html_row.push(['td', html_attrs[i] || {}].concat(this.processInline(row[i].trim()))); + } + table[2].push(html_row); + }, this); - // no match, false alarm - if ( !m ) { - return [ 2, "{:" ]; - } + return [table]; + } - // attach the attributes to the preceeding element - var meta = this.dialect.processMetaHash( m[ 1 ] ), - attr = extract_attr( before ); + Markdown.dialects.Maruku.inline["{:"] = function inline_meta(text, matches, out) { + if (!out.length) { + return [2, "{:"]; + } - if ( !attr ) { - attr = {}; - before.splice( 1, 0, attr ); - } + // get the preceeding element + var before = out[out.length - 1]; - for ( var k in meta ) { - attr[ k ] = meta[ k ]; - } + if (typeof before === "string") { + return [2, "{:"]; + } - // cut out the string and replace it with nothing - return [ m[ 0 ].length, "" ]; -}; + // match a meta hash + var m = text.match(/^\{:\s*((?:\\\}|[^\}])*)\s*\}/); -Markdown.dialects.Maruku.inline.__escape__ = /^\\[\\`\*_{}\[\]()#\+.!\-|:]/; + // no match, false alarm + if (!m) { + return [2, "{:"]; + } -Markdown.buildBlockOrder ( Markdown.dialects.Maruku.block ); -Markdown.buildInlinePatterns( Markdown.dialects.Maruku.inline ); + // attach the attributes to the preceeding element + var meta = this.dialect.processMetaHash(m[1]), + attr = extract_attr(before); -var isArray = Array.isArray || function(obj) { - return Object.prototype.toString.call(obj) == "[object Array]"; -}; + if (!attr) { + attr = {}; + before.splice(1, 0, attr); + } -var forEach; + for (var k in meta) { + attr[k] = meta[k]; + } + + // cut out the string and replace it with nothing + return [m[0].length, ""]; + }; + + Markdown.dialects.Maruku.inline.__escape__ = /^\\[\\`\*_{}\[\]()#\+.!\-|:]/; + + Markdown.buildBlockOrder(Markdown.dialects.Maruku.block); + Markdown.buildInlinePatterns(Markdown.dialects.Maruku.inline); + + var isArray = Array.isArray || function (obj) { + return Object.prototype.toString.call(obj) == "[object Array]"; + }; + + var forEach; // Don't mess with Array.prototype. Its not friendly -if ( Array.prototype.forEach ) { - forEach = function( arr, cb, thisp ) { - return arr.forEach( cb, thisp ); - }; -} -else { - forEach = function(arr, cb, thisp) { - for (var i = 0; i < arr.length; i++) { - cb.call(thisp || arr, arr[i], i, arr); - } - } -} - -var isEmpty = function( obj ) { - for ( var key in obj ) { - if ( hasOwnProperty.call( obj, key ) ) { - return false; - } - } - - return true; -} - -function extract_attr( jsonml ) { - return isArray(jsonml) - && jsonml.length > 1 - && typeof jsonml[ 1 ] === "object" - && !( isArray(jsonml[ 1 ]) ) - ? jsonml[ 1 ] - : undefined; -} - - - -/** - * renderJsonML( jsonml[, options] ) -> String - * - jsonml (Array): JsonML array to render to XML - * - options (Object): options - * - * Converts the given JsonML into well-formed XML. - * - * The options currently understood are: - * - * - root (Boolean): wether or not the root node should be included in the - * output, or just its children. The default `false` is to not include the - * root itself. - */ -expose.renderJsonML = function( jsonml, options ) { - options = options || {}; - // include the root element in the rendered output? - options.root = options.root || false; - - var content = []; - - if ( options.root ) { - content.push( render_tree( jsonml ) ); - } - else { - jsonml.shift(); // get rid of the tag - if ( jsonml.length && typeof jsonml[ 0 ] === "object" && !( jsonml[ 0 ] instanceof Array ) ) { - jsonml.shift(); // get rid of the attributes + if (Array.prototype.forEach) { + forEach = function (arr, cb, thisp) { + return arr.forEach(cb, thisp); + }; + } else { + forEach = function (arr, cb, thisp) { + for (var i = 0; i < arr.length; i++) { + cb.call(thisp || arr, arr[i], i, arr); + } + } } - while ( jsonml.length ) { - content.push( render_tree( jsonml.shift() ) ); - } - } - - return content.join( "\n\n" ); -}; - -function escapeHTML( text ) { - return text.replace( /&/g, "&" ) - .replace( //g, ">" ) - .replace( /"/g, """ ) - .replace( /'/g, "'" ); -} - -function render_tree( jsonml ) { - // basic case - if ( typeof jsonml === "string" ) { - return escapeHTML( jsonml ); - } - - var tag = jsonml.shift(), - attributes = {}, - content = []; - - if ( jsonml.length && typeof jsonml[ 0 ] === "object" && !( jsonml[ 0 ] instanceof Array ) ) { - attributes = jsonml.shift(); - } - - while ( jsonml.length ) { - content.push( render_tree( jsonml.shift() ) ); - } - - var tag_attrs = ""; - for ( var a in attributes ) { - tag_attrs += " " + a + '="' + escapeHTML( attributes[ a ] ) + '"'; - } - - // be careful about adding whitespace here for inline elements - if ( tag == "img" || tag == "br" || tag == "hr" ) { - return "<"+ tag + tag_attrs + "/>"; - } - else { - return "<"+ tag + tag_attrs + ">" + content.join( "" ) + ""; - } -} - -function convert_tree_to_html( tree, references, options ) { - var i; - options = options || {}; - - // shallow clone - var jsonml = tree.slice( 0 ); - - if ( typeof options.preprocessTreeNode === "function" ) { - jsonml = options.preprocessTreeNode(jsonml, references); - } - - // Clone attributes if they exist - var attrs = extract_attr( jsonml ); - if ( attrs ) { - jsonml[ 1 ] = {}; - for ( i in attrs ) { - jsonml[ 1 ][ i ] = attrs[ i ]; - } - attrs = jsonml[ 1 ]; - } - - // basic case - if ( typeof jsonml === "string" ) { - return jsonml; - } - - // convert this node - switch ( jsonml[ 0 ] ) { - case "header": - jsonml[ 0 ] = "h" + jsonml[ 1 ].level; - delete jsonml[ 1 ].level; - break; - case "bulletlist": - jsonml[ 0 ] = "ul"; - break; - case "numberlist": - jsonml[ 0 ] = "ol"; - break; - case "listitem": - jsonml[ 0 ] = "li"; - break; - case "para": - jsonml[ 0 ] = "p"; - break; - case "markdown": - jsonml[ 0 ] = "html"; - if ( attrs ) delete attrs.references; - break; - case "code_block": - jsonml[ 0 ] = "pre"; - i = attrs ? 2 : 1; - var code = [ "code" ]; - code.push.apply( code, jsonml.splice( i, jsonml.length - i ) ); - jsonml[ i ] = code; - break; - case "inlinecode": - jsonml[ 0 ] = "code"; - break; - case "img": - jsonml[ 1 ].src = jsonml[ 1 ].href; - delete jsonml[ 1 ].href; - break; - case "linebreak": - jsonml[ 0 ] = "br"; - break; - case "link": - jsonml[ 0 ] = "a"; - break; - case "link_ref": - jsonml[ 0 ] = "a"; - - // grab this ref and clean up the attribute node - var ref = references[ attrs.ref ]; - - // if the reference exists, make the link - if ( ref ) { - delete attrs.ref; - - // add in the href and title, if present - attrs.href = ref.href; - if ( ref.title ) { - attrs.title = ref.title; + var isEmpty = function (obj) { + for (var key in obj) { + if (hasOwnProperty.call(obj, key)) { + return false; + } } - // get rid of the unneeded original text - delete attrs.original; - } - // the reference doesn't exist, so revert to plain text - else { - return attrs.original; - } - break; - case "img_ref": - jsonml[ 0 ] = "img"; + return true; + } - // grab this ref and clean up the attribute node - var ref = references[ attrs.ref ]; + function extract_attr(jsonml) { + return isArray(jsonml) + && jsonml.length > 1 + && typeof jsonml[1] === "object" + && !(isArray(jsonml[1])) + ? jsonml[1] + : undefined; + } - // if the reference exists, make the link - if ( ref ) { - delete attrs.ref; - // add in the href and title, if present - attrs.src = ref.href; - if ( ref.title ) { - attrs.title = ref.title; + /** + * renderJsonML( jsonml[, options] ) -> String + * - jsonml (Array): JsonML array to render to XML + * - options (Object): options + * + * Converts the given JsonML into well-formed XML. + * + * The options currently understood are: + * + * - root (Boolean): wether or not the root node should be included in the + * output, or just its children. The default `false` is to not include the + * root itself. + */ + expose.renderJsonML = function (jsonml, options) { + options = options || {}; + // include the root element in the rendered output? + options.root = options.root || false; + + var content = []; + + if (options.root) { + content.push(render_tree(jsonml)); + } else { + jsonml.shift(); // get rid of the tag + if (jsonml.length && typeof jsonml[0] === "object" && !(jsonml[0] instanceof Array)) { + jsonml.shift(); // get rid of the attributes + } + + while (jsonml.length) { + content.push(render_tree(jsonml.shift())); + } } - // get rid of the unneeded original text - delete attrs.original; - } - // the reference doesn't exist, so revert to plain text - else { - return attrs.original; - } - break; - } + return content.join("\n\n"); + }; - // convert all the children - i = 1; - - // deal with the attribute node, if it exists - if ( attrs ) { - // if there are keys, skip over it - for ( var key in jsonml[ 1 ] ) { - i = 2; - break; + function escapeHTML(text) { + return text.replace(/&/g, "&") + .replace(//g, ">") + .replace(/"/g, """) + .replace(/'/g, "'"); } - // if there aren't, remove it - if ( i === 1 ) { - jsonml.splice( i, 1 ); + + function render_tree(jsonml) { + // basic case + if (typeof jsonml === "string") { + return escapeHTML(jsonml); + } + + var tag = jsonml.shift(), + attributes = {}, + content = []; + + if (jsonml.length && typeof jsonml[0] === "object" && !(jsonml[0] instanceof Array)) { + attributes = jsonml.shift(); + } + + while (jsonml.length) { + content.push(render_tree(jsonml.shift())); + } + + var tag_attrs = ""; + for (var a in attributes) { + tag_attrs += " " + a + '="' + escapeHTML(attributes[a]) + '"'; + } + + // be careful about adding whitespace here for inline elements + if (tag == "img" || tag == "br" || tag == "hr") { + return "<" + tag + tag_attrs + "/>"; + } else { + return "<" + tag + tag_attrs + ">" + content.join("") + ""; + } } - } - for ( ; i < jsonml.length; ++i ) { - jsonml[ i ] = convert_tree_to_html( jsonml[ i ], references, options ); - } + function convert_tree_to_html(tree, references, options) { + var i; + options = options || {}; - return jsonml; -} + // shallow clone + var jsonml = tree.slice(0); + + if (typeof options.preprocessTreeNode === "function") { + jsonml = options.preprocessTreeNode(jsonml, references); + } + + // Clone attributes if they exist + var attrs = extract_attr(jsonml); + if (attrs) { + jsonml[1] = {}; + for (i in attrs) { + jsonml[1][i] = attrs[i]; + } + attrs = jsonml[1]; + } + + // basic case + if (typeof jsonml === "string") { + return jsonml; + } + + // convert this node + switch (jsonml[0]) { + case "header": + jsonml[0] = "h" + jsonml[1].level; + delete jsonml[1].level; + break; + case "bulletlist": + jsonml[0] = "ul"; + break; + case "numberlist": + jsonml[0] = "ol"; + break; + case "listitem": + jsonml[0] = "li"; + break; + case "para": + jsonml[0] = "p"; + break; + case "markdown": + jsonml[0] = "html"; + if (attrs) delete attrs.references; + break; + case "code_block": + jsonml[0] = "pre"; + i = attrs ? 2 : 1; + var code = ["code"]; + code.push.apply(code, jsonml.splice(i, jsonml.length - i)); + jsonml[i] = code; + break; + case "inlinecode": + jsonml[0] = "code"; + break; + case "img": + jsonml[1].src = jsonml[1].href; + delete jsonml[1].href; + break; + case "linebreak": + jsonml[0] = "br"; + break; + case "link": + jsonml[0] = "a"; + break; + case "link_ref": + jsonml[0] = "a"; + + // grab this ref and clean up the attribute node + var ref = references[attrs.ref]; + + // if the reference exists, make the link + if (ref) { + delete attrs.ref; + + // add in the href and title, if present + attrs.href = ref.href; + if (ref.title) { + attrs.title = ref.title; + } + + // get rid of the unneeded original text + delete attrs.original; + } + // the reference doesn't exist, so revert to plain text + else { + return attrs.original; + } + break; + case "img_ref": + jsonml[0] = "img"; + + // grab this ref and clean up the attribute node + var ref = references[attrs.ref]; + + // if the reference exists, make the link + if (ref) { + delete attrs.ref; + + // add in the href and title, if present + attrs.src = ref.href; + if (ref.title) { + attrs.title = ref.title; + } + + // get rid of the unneeded original text + delete attrs.original; + } + // the reference doesn't exist, so revert to plain text + else { + return attrs.original; + } + break; + } + + // convert all the children + i = 1; + + // deal with the attribute node, if it exists + if (attrs) { + // if there are keys, skip over it + for (var key in jsonml[1]) { + i = 2; + break; + } + // if there aren't, remove it + if (i === 1) { + jsonml.splice(i, 1); + } + } + + for (; i < jsonml.length; ++i) { + jsonml[i] = convert_tree_to_html(jsonml[i], references, options); + } + + return jsonml; + } // merges adjacent text nodes into a single node -function merge_text_nodes( jsonml ) { - // skip the tag name and attribute hash - var i = extract_attr( jsonml ) ? 2 : 1; + function merge_text_nodes(jsonml) { + // skip the tag name and attribute hash + var i = extract_attr(jsonml) ? 2 : 1; - while ( i < jsonml.length ) { - // if it's a string check the next item too - if ( typeof jsonml[ i ] === "string" ) { - if ( i + 1 < jsonml.length && typeof jsonml[ i + 1 ] === "string" ) { - // merge the second string into the first and remove it - jsonml[ i ] += jsonml.splice( i + 1, 1 )[ 0 ]; - } - else { - ++i; - } + while (i < jsonml.length) { + // if it's a string check the next item too + if (typeof jsonml[i] === "string") { + if (i + 1 < jsonml.length && typeof jsonml[i + 1] === "string") { + // merge the second string into the first and remove it + jsonml[i] += jsonml.splice(i + 1, 1)[0]; + } else { + ++i; + } + } + // if it's not a string recurse + else { + merge_text_nodes(jsonml[i]); + ++i; + } + } } - // if it's not a string recurse - else { - merge_text_nodes( jsonml[ i ] ); - ++i; - } - } -} -} )( (function() { - if ( typeof exports === "undefined" ) { - window.markdown = {}; - return window.markdown; - } - else { - return exports; - } -} )() ); +})((function () { + if (typeof exports === "undefined") { + window.markdown = {}; + return window.markdown; + } else { + return exports; + } +})()); diff --git a/public/traitementEnvoiLienVeilleTinternet.php b/public/traitementEnvoiLienVeilleTinternet.php index 2463933..63cc454 100644 --- a/public/traitementEnvoiLienVeilleTinternet.php +++ b/public/traitementEnvoiLienVeilleTinternet.php @@ -1,65 +1,68 @@ $line) { - if(preg_match("#define\(HASH,(.*)\)#",$line,$matches)){ - $newline = preg_replace("#define\(HASH,(.*)\)#","define(HASH,'$hash')",$line); - }else{ - $newline = $line; - } - if ($newline != "") { + $file = ""; + $lines = file(APPLICATION_PATH . DIRECTORY_SEPARATOR . "parameters.php"); + foreach ($lines as $line_num => $line) { + if (preg_match("#define\(HASH,(.*)\)#", $line, $matches)) { + $newline = preg_replace("#define\(HASH,(.*)\)#", "define(HASH,'$hash')", $line); + } else { + $newline = $line; + } + if ($newline != "") { $file .= $newline . "\n"; } - } - /* Store new hash in parameters file */ - file_put_contents(APPLICATION_PATH . DIRECTORY_SEPARATOR . "parameters.php",$file); - } - } - //try fallback passengers - if (password_verify($password,HASH)) { - return true; - } else { - return false; - } + } + /* Store new hash in parameters file */ + file_put_contents(APPLICATION_PATH . DIRECTORY_SEPARATOR . "parameters.php", $file); + } + } + //try fallback passengers + if (password_verify($password, HASH)) { + return true; + } else { + return false; + } } //https://code.tutsplus.com/tutorials/8-regular-expressions-you-should-know--net-6149 -function extractLink($message){ - preg_match_all("#((?:https?:\/\/)?(?:[\da-z\.-]+)\.(?:[a-z\.]{2,6})(?:[\/\w \.-]*)*\/?)#",$message,$matches); - return $matches; +function extractLink($message) +{ + preg_match_all("#((?:https?:\/\/)?(?:[\da-z\.-]+)\.(?:[a-z\.]{2,6})(?:[\/\w \.-]*)*\/?)#", $message, $matches); + return $matches; } -function extractText($message){ - $links = extractLink($message); - array_shift($links); - foreach ($links as $link) { - if ($link != "") { - $message = preg_replace("#$link[0]#"," ... ",$message); - } - } - return $message; +function extractText($message) +{ + $links = extractLink($message); + array_shift($links); + foreach ($links as $link) { + if ($link != "") { + $message = preg_replace("#$link[0]#", " ... ", $message); + } + } + return $message; } /**************************************************************************************************************/ @@ -67,11 +70,11 @@ function extractText($message){ $message = Encoding::fixUTF8(Encoding::protectionDoubleQuote(Encoding::protectionSimpleQuote($_POST['message']))); -if (passValide($_POST['password']) && $_POST['message'] != ""){ +if (passValide($_POST['password']) && $_POST['message'] != "") { - $hookMattermost = "http://{your-mattermost-site}/hooks/xxx-generatedkey-xxx"; + $hookMattermost = "http://{your-mattermost-site}/hooks/xxx-generatedkey-xxx"; - $payload = <<"; - print_r($commandJSON); - echo "
    ";
    -	print_r($commandURL);
    +    echo "
    ";
    +    print_r($commandJSON);
    +    echo "
    ";
    +    print_r($commandURL);
     
    -	$dom = new DOMDocument;
    -	$dom->load(PUBLIC_PATH . DIRECTORY_SEPARATOR ."veilleTinternet.xml");
    +    $dom = new DOMDocument;
    +    $dom->load(PUBLIC_PATH . DIRECTORY_SEPARATOR . "veilleTinternet.xml");
     
    -	$noeudVeille = $dom->getElementsByTagName("veille")->item(0);
    +    $noeudVeille = $dom->getElementsByTagName("veille")->item(0);
     
    -	$messageNode = $dom->createElement("message");
    +    $messageNode = $dom->createElement("message");
     
         $myOriginalTextNode = $dom->createElement("originel");
         $textNode = $dom->createTextNode(htmlentities($message));
         $myOriginalTextNode->appendChild($textNode);
         $messageNode->appendChild($myOriginalTextNode);
     
    -	$myTextNode = $dom->createElement("text");
    -	$textNode = $dom->createTextNode(htmlentities(extractText($message)));
    -	$myTextNode->appendChild($textNode);
    +    $myTextNode = $dom->createElement("text");
    +    $textNode = $dom->createTextNode(htmlentities(extractText($message)));
    +    $myTextNode->appendChild($textNode);
         $messageNode->appendChild($myTextNode);
     
         $myLinksNode = $dom->createElement("links");
    -	$links = extractLink($message);
    +    $links = extractLink($message);
         array_shift($links);
    -	foreach($links as $link) {
    -	    if($link != "") {
    +    foreach ($links as $link) {
    +        if ($link != "") {
                 $myLinkNode = $dom->createElement("link");
                 $textNode = $dom->createTextNode(htmlentities($link[0]));
                 $myLinkNode->appendChild($textNode);
    @@ -129,21 +132,21 @@ EOD;
         //$dom->insertBefore( $messageNode, $noeudVeille );
         $noeudVeille->appendChild($messageNode);
     
    -	$dom->save(PUBLIC_PATH . DIRECTORY_SEPARATOR ."veilleTinternet.xml");
    +    $dom->save(PUBLIC_PATH . DIRECTORY_SEPARATOR . "veilleTinternet.xml");
     
         shell_exec($commandJSON);
         shell_exec($commandURL);
         die();
    -	if(isset($_POST['ajax'])){
    -		echo "Votre Lien as été correctement transmis. Vous receverez une réponse dans les prochains jours.";
    -	}else{
    -		header("Location: /index.php?envoiDuMessage=oui");
    -	}
    +    if (isset($_POST['ajax'])) {
    +        echo "Votre Lien as été correctement transmis. Vous receverez une réponse dans les prochains jours.";
    +    } else {
    +        header("Location: /index.php?envoiDuMessage=oui");
    +    }
     
    -}else{
    -	if(isset($_POST['ajax'])){
    -		echo "Votre Lien n'as pas été transmis. Veuillez vérifiez les informations que vous avez saisies ...";
    -	}else{
    -		header("Location: /index.php?envoiDuMessage=non&message=".Encoding::myUrlEncode($message));
    -	}
    +} else {
    +    if (isset($_POST['ajax'])) {
    +        echo "Votre Lien n'as pas été transmis. Veuillez vérifiez les informations que vous avez saisies ...";
    +    } else {
    +        header("Location: /index.php?envoiDuMessage=non&message=" . Encoding::myUrlEncode($message));
    +    }
     }
    \ No newline at end of file