Skip to content

Commit

Permalink
[Add] Use Pangu.php instead of Pangu.js
Browse files Browse the repository at this point in the history
  • Loading branch information
idawnlight committed Oct 21, 2017
1 parent c7a9447 commit 208259c
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 2 deletions.
10 changes: 9 additions & 1 deletion footer.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ function() {
</script>
<?php endif; ?>

<?php if (!empty($this->options->switch) && in_array('Pangu', $this->options->switch)): ?>
<?php if (!empty($this->options->switch) && in_array('Pangu', $this->options->switch) && !in_array('PanguPHP', $this->options->switch)): ?>
<!-- Pangu -->
<?php if (!empty($this->options->CDNURL)): ?>
<script src="<?php $this->options->CDNURL() ?>/MaterialCDN/js/pangu.min.js"></script>
Expand Down Expand Up @@ -332,6 +332,14 @@ function() {
</script>
<?php endif; ?>

<?php
if($this->is('post')||$this->is('page')){
if (!empty($this->options->switch) && in_array('PanguPHP', $this->options->switch)) {
$html_source = ob_get_contents(); ob_clean(); print pangu::do($html_source); ob_end_flush();
}
}
?>

<?php $this->footer(); ?>

</body>
Expand Down
4 changes: 3 additions & 1 deletion functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
define("MATERIAL_VERSION", "3.1.0");

require_once("lib/UACheck.php");
require_once("lib/pangu.php");

/**
* JavaScript 载入
Expand Down Expand Up @@ -74,11 +75,12 @@ function themeConfig($form)
'ShowLoadingLine' => _t('顶部 loading 加载进度条效果'),
'atargetblank' => _t('链接以新标签页形式打开'),
'Pangu' => _t('引用 Pangu.js 实现中英文间自动添加空格'),
'PanguPHP' => _t('引用 Pangu.PHP 后端实现中英文间自动添加空格'),
'HighLight' => _t('引用 highlight.js 实现代码高亮')
),

//Default choose
array('SmoothScroll','ShowLoadingLine','Pangu','HighLight'), _t('<span style="font-size: large" id="function"><strong>功能设定</strong></span><br /><br />功能开关')
array('SmoothScroll','ShowLoadingLine','PanguPHP','HighLight'), _t('<span style="font-size: large" id="function"><strong>功能设定</strong></span><br /><br />功能开关')
);
$form->addInput($switch->multiMode());

Expand Down
115 changes: 115 additions & 0 deletions lib/pangu.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php
/**
* pangu.php
* https://github.com/kunr/pangu.php
*
* Copyright (c) 2015 Rakume Hayashi
* Licensed under the MIT License
*/

class pangu
{
public static function do($text) {
$cjk = '' .
'\x{2e80}-\x{2eff}' .
'\x{2f00}-\x{2fdf}' .
'\x{3040}-\x{309f}' .
'\x{30a0}-\x{30ff}' .
'\x{3100}-\x{312f}' .
'\x{3200}-\x{32ff}' .
'\x{3400}-\x{4dbf}' .
'\x{4e00}-\x{9fff}' .
'\x{f900}-\x{faff}';

$patterns = array(
'cjk_quote' => array(
'([' . $cjk . '])(["\'])',
'$1 $2'
),

'quote_cjk' => array(
'(["\'])([' . $cjk . '])',
'$1 $2'
),

'fix_quote' => array(
'(["\']+)(\s*)(.+?)(\s*)(["\']+)',
'$1$3$5'
),

'cjk_hash' => array(
'([' . $cjk . '])(#(\S+))',
'$1 $2'
),

'hash_cjk' => array(
'((\S+)#)([' . $cjk . '])',
'$1 $3'
),

'cjk_operator_ans' => array(
'([' . $cjk . '])([A-Za-z0-9])([\+\-\*\/=&\\|<>])',
'$1 $2 $3'
),

'ans_operator_cjk' => array(
'([\+\-\*\/=&\\|<>])([A-Za-z0-9])([' . $cjk . '])',
'$1 $2 $3'
),

'bracket' => array(
array(
'([' . $cjk . '])([<\[\{\(]+(.*?)[>\]\}\)]+)([' . $cjk . '])',
'$1 $2 $4'
),

array(
'cjk_bracket' => array(
'([' . $cjk . '])([<>\[\]\{\}\(\)])',
'$1 $2'
),

'bracket_cjk' => array(
'([<>\[\]\{\}\(\)])([' . $cjk . '])',
'$1 $2'
)
)
),

'fix_bracket' => array(
'([<\[\{\(]+)(\s*)(.+?)(\s*)([>\]\}\)]+)',
'$1$3$5'
),

'cjk_ans' => array(
'([' . $cjk . '])([A-Za-z0-9`@&%\=\$\^\*\-\+\\/|\\\])',
'$1 $2'
),

'ans_cjk' => array(
'([A-Za-z0-9`~!%&=;\|\,\.\:\?\$\^\*\-\+\/\\\])([' . $cjk . '])',
'$1 $2'
)
);

foreach ($patterns as $key => $value) {
if ($key === 'bracket') {
$old = $text;
$new = preg_replace('/' . $value[0][0] . '/iu', $value[0][1], $text);
$text = $new;

if ($old === $new) {
foreach ($value[1] as $value) {
$text = preg_replace('/' . $value[0] . '/iu', $value[1], $text);
}
}

continue;
}

$text = preg_replace('/' . $value[0] . '/iu', $value[1], $text);
}

return $text;
}
}

0 comments on commit 208259c

Please sign in to comment.