Skip to content

Instantly share code, notes, and snippets.

@aoitaku
Last active April 21, 2019 08:58
Show Gist options
  • Save aoitaku/d289ffd920408d1c24a3 to your computer and use it in GitHub Desktop.
Save aoitaku/d289ffd920408d1c24a3 to your computer and use it in GitHub Desktop.
cebe\markdownを拡張してMarkdownにルビと傍点の記法を追加する
<?php
class MarkdownStory extends cebe\markdown\Markdown
{
/**
* @var boolean whether to interpret newlines as `<br />`-tags.
* This feature is useful for comments where newlines are often meant to be real new lines.
*/
public $enableNewlines = true;
/**
* @inheritDoc
*/
protected $escapeCharacters = [
// from Markdown
'\\', // backslash
'`', // backtick
'*', // asterisk
'_', // underscore
'{', '}', // curly braces
'[', ']', // square brackets
'(', ')', // parentheses
'#', // hash mark
'+', // plus sign
'-', // minus sign (hyphen)
'.', // dot
'!', // exclamation mark
'<', '>',
// from GithubMarkdown
':', // colon
'|', // pipe
// added by Yarkdown
'^', // circumflex
];
private $_specialAttributesRegex = '\{(([#\.][A-z0-9-_]+\s*)+)\}';
protected $first_section = true;
/**
* @inheritDoc
*/
protected function inlineMarkers()
{
return parent::inlineMarkers() + [
'^' => 'parseRuby',
'.' => 'parseEmphDot',
];
}
// inline parsing
/**
* Parses the ruby annotation feature.
*/
protected function parseRuby($markdown)
{
if (preg_match('/^\^(.+?)\((.+?)\)/', $markdown, $matches)) {
return [
"<ruby><rb>{$matches[1]}</rb><rp>(</rp><rt>{$matches[2]}</rt><rp>)</rp></ruby>",
strlen($matches[0])
];
}
return [$markdown[0] . $markdown[1], 2];
}
/**
* Parses the emphasis dot feature.
*/
protected function parseEmphDot($markdown)
{
if (preg_match('/^\.\.(.+?)\.\./', $markdown, $matches)) {
return [
implode('', array_map(function($char){
return "<em><span>{$char}</span></em>";
}, preg_split("//u", $matches[1], -1, PREG_SPLIT_NO_EMPTY))),
strlen($matches[0])
];
}
return [$markdown[0] . $markdown[1], 2];
}
}
@aoitaku
Copy link
Author

aoitaku commented Sep 23, 2014

  ^テキスト(ルビ)
  ..傍点..

という感じで書いて使う。$enableNewlinesをtrueにすると改行でbrに、空行で段落を分けるようになる。いちいち空白を行末に二個置いたりしなくていいので便利。日本語にはこのほうが適してると思う。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment