⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 markdown.php

📁 是一个优秀的语义个人发布平台
💻 PHP
📖 第 1 页 / 共 2 页
字号:
<?php
/*
Plugin Name: MarkDown
Plugin URI: http://daringfireball.net/projects/markdown/
Description: Markdown is a text-to-HTML conversion tool for web writers. <a href="http://daringfireball.net/projects/markdown/syntax">Markdown syntax</a> allows you to write using an easy-to-read, easy-to-write plain text format, then convert it to structurally valid XHTML. This plugin <strong>enables Markdown for your posts and comments</strong>. Written by <a href="http://daringfireball.net/">John Gruber</a> in Perl, translated to PHP by <a href="http://www.michelf.com/">Michel Fortin</a>, and made a WP plugin by <a href="http://photomatt.net/">Matt</a>. If you use this you should disable Textile 1 and 2 because the syntax conflicts.
Version: 1.0b4
Author: John Gruber
Author URI: http://daringfireball.net/
*/ 


/*
Note to code readers: I've stripped most of the comments from the source, see the original at http://www.michelf.com/php-markdown/?code to get the unaltered version. --Matt
*/

$MarkdownPHPVersion    = '1.0b4.1'; # Sun 4 Apr 2004
$MarkdownSyntaxVersion = '1.0b4'; # Thu 25 Mar 2004
$g_empty_element_suffix = " />";     # Change to ">" for HTML output
$g_tab_width = 4;
$g_nested_brackets_depth = 6;
$g_nested_brackets = 
	str_repeat('(?>[^\[\]]+|\[', $g_nested_brackets_depth).
	str_repeat('\])*', $g_nested_brackets_depth);
$g_escape_table = array(
	"\\" => md5("\\"),
	"`" => md5("`"),
	"*" => md5("*"),
	"_" => md5("_"),
	"{" => md5("{"),
	"}" => md5("}"),
	"[" => md5("["),
	"]" => md5("]"),
	"(" => md5("("),
	")" => md5(")"),
	"#" => md5("#"),
	"." => md5("."),
	"!" => md5("!")
);
$g_backslash_escape_table;
foreach ($g_escape_table as $key => $char)
	$g_backslash_escape_table["\\$key"] = $char;

$g_urls;
$g_titles;
$g_html_blocks;

function Markdown($text) {
	global $g_urls, $g_titles, $g_html_blocks;
	$g_urls = array();
	$g_titles = array();
	$g_html_blocks = array();
	$text = str_replace(array("\r\n", "\r"), "\n", $text);
	$text .= "\n\n";
	$text = _Detab($text);
	$text = preg_replace('/^[ \t]+$/m', '', $text);
	$text = _HashHTMLBlocks($text);
	$text = _StripLinkDefinitions($text);
	$text = _EscapeSpecialChars($text);
	$text = _RunBlockGamut($text);
	$text = _UnescapeSpecialChars($text);
	return $text . "\n";
}

function _StripLinkDefinitions($text) {
	$text = preg_replace_callback('{
						^[ \t]*\[(.+)\]:	# id = $1
						  [ \t]*
						  \n?				# maybe *one* newline
						  [ \t]*
						(\S+)				# url = $2
						  [ \t]*
						  \n?				# maybe one newline
						  [ \t]*
						(?:
							# Todo: Titles are delimited by "quotes" or (parens).
							["(]
							(.+?)			# title = $3
							[")]
							[ \t]*
						)?	# title is optional
						(?:\n+|\Z)
		}xm',
		'_StripLinkDefinitions_callback',
		$text);
	return $text;
}
function _StripLinkDefinitions_callback($matches) {
	global $g_urls, $g_titles;
	$link_id = strtolower($matches[1]);
	$g_urls[$link_id] = _EncodeAmpsAndAngles($matches[2]);
	if (isset($matches[3]))
		$g_titles[$link_id] = htmlentities($matches[3]);
	return ''; # String that will replace the block
}

function _HashHTMLBlocks($text) {
	$block_tag_re = 'p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script';
	$text = preg_replace_callback("{
				(						# save in $1
					^					# start of line  (with /m)
					<($block_tag_re)	# start tag = $2
					\\b					# word break
					(.*\\n)*?			# any number of lines, minimally matching
					</\\2>				# the matching end tag
					[ \\t]*				# trailing spaces/tabs
					(?=\\n+|\\Z)	# followed by a newline or end of document
				)
		}xm",
		'_HashHTMLBlocks_callback',
		$text);

	$text = preg_replace_callback("{
				(						# save in $1
					^					# start of line  (with /m)
					<($block_tag_re)	# start tag = $2
					\\b					# word break
					(.*\\n)*?			# any number of lines, minimally matching
					.*</\\2>				# the matching end tag
					[ \\t]*				# trailing spaces/tabs
					(?=\\n+|\\Z)	# followed by a newline or end of document
				)
		}xm",
		'_HashHTMLBlocks_callback',
		$text);

	$text = preg_replace_callback('{
				(?:
					(?<=\n\n)		# Starting after a blank line
					|				# or
					\A\n?			# the beginning of the doc
				)
				(						# save in $1
					[ \t]*
					<(hr)				# start tag = $2
					\b					# word break
					([^<>])*?			# 
					/?>					# the matching end tag
					(?=\n{2,}|\Z)		# followed by a blank line or end of document
				)
		}x',
		'_HashHTMLBlocks_callback',
		$text);

	return $text;
}
function _HashHTMLBlocks_callback($matches) {
	global $g_html_blocks;
	$text = $matches[1];
	$key = md5($text);
	$g_html_blocks[$key] = $text;
	return "\n\n$key\n\n"; # String that will replace the block
}

function _RunBlockGamut($text) {
	global $g_empty_element_suffix;
	
	$text = _DoHeaders($text);

	$text = preg_replace(
		array('/^( ?\* ?){3,}$/m',
			  '/^( ?- ?){3,}$/m'),
		array("\n<hr$g_empty_element_suffix\n", 
			  "\n<hr$g_empty_element_suffix\n"), 
		$text);

	$text = _DoLists($text);

	$text = _DoCodeBlocks($text);

	$text = _DoBlockQuotes($text);

	$text = _DoAutoLinks($text);

	$text = _HashHTMLBlocks($text);

	$text = _FormParagraphs($text);

	return $text;
}


function _RunSpanGamut($text) {
	global $g_empty_element_suffix;
	$text = _DoCodeSpans($text);


	$text = _EncodeAmpsAndAngles($text);

	$text = _DoImages($text);
	$text = _DoAnchors($text);


	$text = _DoItalicsAndBold($text);
	
	# Do hard breaks:
	$text = preg_replace('/ {2,}\n/', "<br$g_empty_element_suffix\n", $text);

	return $text;
}


function _EscapeSpecialChars($text) {
	global $g_escape_table;
	$tokens = _TokenizeHTML($text);

	$text = '';   # rebuild $text from the tokens
	$in_pre = 0;  # Keep track of when we're inside <pre> or <code> tags.
	$tags_to_skip = "!<(/?)(?:pre|code|kbd|script)[\s>]!";

	foreach ($tokens as $cur_token) {
		if ($cur_token[0] == 'tag') {
			$cur_token[1] = str_replace(array('*', '_'),
				array($g_escape_table['*'], $g_escape_table['_']),
				$cur_token[1]);
			$text .= $cur_token[1];
		} else {
			$t = $cur_token[1];
			if (! $in_pre) {
				$t = _EncodeBackslashEscapes($t);
				# $t =~ s{([a-z])/([a-z])}{$1&thinsp;/&thinsp;$2}ig;
			}
			$text .= $t;
		}
	}
	return $text;
}


function _DoAnchors($text) {
	global $g_nested_brackets;

	$text = preg_replace_callback("{
		(					# wrap whole match in $1
		  \\[
		    ($g_nested_brackets)	# link text = $2
		  \\]

		  [ ]?				# one optional space
		  (?:\\n[ ]*)?		# one optional newline followed by spaces

		  \\[
		    (.*?)		# id = $3
		  \\]
		)
		}xs",
		'_DoAnchors_reference_callback', $text);
	
	$text = preg_replace_callback("{
		(				# wrap whole match in $1
		  \\[
			($g_nested_brackets)	# link text = $2
		  \\]
		  \\(			# literal paren
			[ \\t]*
			(.+?)		# href = $3
			[ \\t]*
			(			# title = $4
			  (['\"])	# quote char = $5
			  .*?
			  \\5		# matching quote
			)?			# title is optional
		  \\)
		)
		}xs",
		'_DoAnchors_inline_callback', $text);
	
	return $text;
}
function _DoAnchors_reference_callback($matches) {
	global $g_urls, $g_titles;
	$result;
	$whole_match = $matches[1];
	$link_text   = $matches[2];
	$link_id     = strtolower($matches[3]);

	if ($link_id == "") {
		$link_id = strtolower($link_text); # for shortcut links like [this][].
	}

	if (isset($g_urls[$link_id])) {
		$url = $g_urls[$link_id];
		$url = str_replace(array('*',     '_'),
						   array('&#42;', '&#95;'), $url);
		$result = "<a href='$url'";
		if ( isset( $g_title[$link_id] ) ) {
			$title = $g_titles[$link_id];
			$title = str_replace(array('*',     '_'),
								 array('&#42;', '&#95;'), $title);
			$result .=  " title=\"$title\"";
		}
		$result .= ">$link_text</a>";
	}
	else {
		$result = $whole_match;
	}
	return $result;
}
function _DoAnchors_inline_callback($matches) {
	$result;
	$whole_match = $matches[1];
	$link_text   = $matches[2];
	$url	  		= $matches[3];
	$title		= $matches[4];

	# We've got to encode these to avoid conflicting with italics/bold.
	$url = str_replace(array('*',     '_'),
					   array('&#42;', '&#95;'), $url);
	$result = "<a href=\"$url\"";
	if ($title) {
		$title = str_replace(array('*',     '_'),
							 array('&#42;', '&#95;'), $title);
		$result .=  " title=$title";
	}
	$result .= ">$link_text</a>";

	return $result;
}


function _DoImages($text) {
	$text = preg_replace_callback('{
		(				# wrap whole match in $1
		  !\[
		    (.*?)		# alt text = $2
		  \]

		  [ ]?				# one optional space
		  (?:\n[ ]*)?		# one optional newline followed by spaces

		  \[
		    (.*?)		# id = $3
		  \]

		)
		}xs', 
		'_DoImages_reference_callback', $text);

	#
	# Next, handle inline images:  ![alt text](url "optional title")
	# Don't forget: encode * and _

	$text = preg_replace_callback("{
		(				# wrap whole match in $1
		  !\\[
			(.*?)		# alt text = $2
		  \\]
		  \\(			# literal paren
			[ \\t]*
			(\\S+)		# src url = $3
			[ \\t]*
			(			# title = $4
			  (['\"])	# quote char = $5
			  .*?
			  \\5		# matching quote
			  [ \\t]*
			)?			# title is optional
		  \\)
		)
		}xs",
		'_DoImages_inline_callback', $text);

	return $text;
}
function _DoImages_reference_callback($matches) {
	global $g_urls, $g_titles, $g_empty_element_suffix;
	$result;
	$whole_match = $matches[1];
	$alt_text    = $matches[2];
	$link_id     = strtolower($matches[3]);

	if ($link_id == "") {
		$link_id = strtolower($alt_text); # for shortcut links like ![this][].
	}
	
	if (isset($g_urls[$link_id])) {
		$url = $g_urls[$link_id];
		$url = str_replace(array('*',     '_'),
						   array('&#42;', '&#95;'), $url);
		$result = "<img src=\"$url\" alt=\"$alt_text\"";
		if (isset($g_titles[$link_id])) {
			$title = $g_titles[$link_id];
			$title = str_replace(array('*',     '_'),
								 array('&#42;', '&#95;'), $title);
			$result .=  " title=\"$title\"";
		}
		$result .= $g_empty_element_suffix;
	}
	else {
		$result = $whole_match;
	}

	return $result;
}
function _DoImages_inline_callback($matches) {
	global $g_empty_element_suffix;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -