Skip to content

Commit 1fb22fb

Browse files
Merge pull request #121 from ContentsViewer/pre
OutlineText: list と definition list のタグ閉じの衝突修正
2 parents f349543 + 3ba3d75 commit 1fb22fb

File tree

1 file changed

+93
-63
lines changed

1 file changed

+93
-63
lines changed

Module/OutlineText.php

Lines changed: 93 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ public static function OnReset() {}
2222

2323
public static function OnEmptyLine($context, &$output) {$output = '';return false;}
2424

25+
public static function OnNewLine($context, &$output) {$output = '';return false;}
26+
2527
public static function OnPreBeginLine($context, &$output) {$output = '';return false;}
2628

2729
public static function OnBeginLine($context, &$output) {$output = '';return false;}
2830

2931
public static function OnIndent($context, &$output) {$output = '';return false;}
3032

31-
public static function OnUnindent($context, &$output) {$output = '';return false;}
32-
33-
public static function OnUnchangedIndent($context, &$output) {$output = '';return false;}
33+
public static function OnOutdent($context, &$output) {$output = '';return false;}
3434

3535
public static function OnEndOfDocument($context, &$output) {$output = '';return false;}
3636
}
@@ -354,7 +354,7 @@ public static function OnIndent($context, &$output) {
354354
return false;
355355
}
356356

357-
public static function OnUnindent($context, &$output) {
357+
public static function OnOutdent($context, &$output) {
358358
$output = '';
359359

360360
// セクションから抜ける前に空行を入れる
@@ -407,6 +407,24 @@ public static function OnEndOfDocument($context, &$output){
407407
return false;
408408
}
409409

410+
public static function OnPreBeginLine($context, &$output){
411+
$output = '';
412+
413+
$line = $context->CurrentLine();
414+
415+
if(preg_match("/(.*):$/", $line, $matches)){
416+
// OnBeginLine で実行
417+
}
418+
else{
419+
if(static::GetLatestIndent() == $context->indentLevel){
420+
$output .= '</dl>';
421+
array_pop(static::$indentStack);
422+
static::$indentStackCount--;
423+
}
424+
}
425+
return false;
426+
}
427+
410428
public static function OnBeginLine($context, &$output){
411429
$output = '';
412430

@@ -439,11 +457,7 @@ public static function OnBeginLine($context, &$output){
439457
}
440458
}
441459
else{
442-
if(static::GetLatestIndent() == $context->indentLevel){
443-
$output .= '</dl>';
444-
array_pop(static::$indentStack);
445-
static::$indentStackCount--;
446-
}
460+
// OnPreBeginLine で実行
447461
}
448462
return false;
449463
}
@@ -463,7 +477,7 @@ public static function OnIndent($context, &$output){
463477
return false;
464478
}
465479

466-
public static function OnUnindent($context, &$output){
480+
public static function OnOutdent($context, &$output){
467481
$output = '';
468482

469483
if($context->indentLevel == static::GetLatestIndent()){
@@ -541,7 +555,7 @@ public static function OnIndent($context, &$output) {
541555
return false;
542556
}
543557

544-
public static function OnUnindent($context, &$output) {
558+
public static function OnOutdent($context, &$output) {
545559
$output = '';
546560

547561
$currentChunk = $context->CurrentChunk();
@@ -563,6 +577,37 @@ public static function OnUnindent($context, &$output) {
563577
return false;
564578
}
565579

580+
public static function OnPreBeginLine($context, &$output){
581+
$output = '';
582+
583+
$currentChunk = $context->CurrentChunk();
584+
585+
if(static::$listStackCount <= 0){
586+
// OnBeginLine で実行
587+
}
588+
elseif(
589+
($list = static::GetLatestList()) !== false &&
590+
$list['indentLevel'] == $currentChunk['indentLevel']
591+
){
592+
if(
593+
preg_match("/^\* /", $currentChunk["content"]) ||
594+
preg_match("/^\+ /", $currentChunk["content"]) ||
595+
preg_match("/^([a-zA-Z0-9]+\.)+ /", $currentChunk["content"])
596+
){
597+
// OnBeginLine で実行
598+
}
599+
else{
600+
// このレベルのリスト終了
601+
$list = array_pop(static::$listStack);
602+
static::$listStackCount--;
603+
604+
$output .= '</li>' . $list['endTag'];
605+
return false;
606+
}
607+
}
608+
return false;
609+
}
610+
566611
public static function OnBeginLine($context, &$output) {
567612
$output = '';
568613

@@ -596,12 +641,7 @@ public static function OnBeginLine($context, &$output) {
596641
return true;
597642
}
598643
else{
599-
// このレベルのリスト終了
600-
$list = array_pop(static::$listStack);
601-
static::$listStackCount--;
602-
603-
$output .= '</li>' . $list['endTag'];
604-
return false;
644+
// OnPreBeginLineで実行
605645
}
606646
}
607647
elseif(
@@ -714,7 +754,7 @@ public static function OnEmptyLine($context, &$output) {
714754
return false;
715755
}
716756

717-
public static function OnPreBeginLine($context, &$output) {
757+
public static function OnNewLine($context, &$output) {
718758
$output = '';
719759

720760
if (static::$isBeginRow) {
@@ -852,6 +892,7 @@ private static function IsTableLine($context) {
852892
class HeadingElementParser extends ElementParser {
853893
private static $isBegin = false;
854894
private static $heading = '';
895+
private static $level;
855896
private static $nextLineIsHorizontalLine = false;
856897

857898
public static function OnReset() {
@@ -862,18 +903,18 @@ public static function OnEmptyLine($context, &$output) {
862903
$output = '';
863904

864905
if (static::$isBegin) {
865-
$output .= '</h' . ($context->indentLevel + 2) . '>';
906+
$output .= '</h' . static::$level . '>';
866907
static::$isBegin = false;
867908
}
868909

869910
return false;
870911
}
871912

872-
public static function OnPreBeginLine($context, &$output) {
913+
public static function OnNewLine($context, &$output) {
873914
$output = '';
874915

875916
if (static::$isBegin) {
876-
$output .= '</h' . ($context->indentLevel + 2) . '>';
917+
$output .= '</h' . static::$level . '>';
877918
static::$isBegin = false;
878919
}
879920

@@ -884,17 +925,8 @@ public static function OnBeginLine($context, &$output) {
884925
$output = '';
885926

886927
if (static::IsHeadingLine($context)) {
887-
$output .= '<h' . ($context->indentLevel + 2) . '>';
888-
// if ($context->indentLevel <= 0) {
889-
// $output .= "class = 'section-title'>";
890-
// } elseif ($context->indentLevel == 1) {
891-
// $output .= "class = 'sub-section-title'>";
892-
// } elseif ($context->indentLevel == 2) {
893-
// $output .= "class = 'sub-sub-section-title'>";
894-
// } else {
895-
// $output .= "class = 'sub-sub-sub-section-title'>";
896-
// }
897-
928+
static::$level = $context->indentLevel + 2;
929+
$output .= '<h' . static::$level . '>';
898930
$output .= Parser::DecodeSpanElements(static::$heading, $context);
899931

900932
if (static::$nextLineIsHorizontalLine) {
@@ -1169,17 +1201,21 @@ class Parser {
11691201
'DefinitionListElementParser',
11701202
];
11711203

1172-
private static $onPreBeginLineParserList = [
1204+
private static $onNewLineParserList = [
11731205
'HeadingElementParser',
1174-
'BoxElementParser',
11751206
'TableElementParser',
1207+
];
1208+
1209+
private static $onPreBeginLineParserList = [
11761210
'ListElementParser',
1211+
'DefinitionListElementParser',
1212+
'BoxElementParser',
11771213
];
11781214

11791215
private static $onBeginLineParserList = [
1180-
'HeadingElementParser',
1181-
'DefinitionListElementParser',
11821216
'ListElementParser',
1217+
'DefinitionListElementParser',
1218+
'HeadingElementParser',
11831219
'BoxElementParser',
11841220
'BlockquoteElementParser',
11851221
'HorizontalLineElementParser',
@@ -1206,16 +1242,12 @@ class Parser {
12061242
'SectionElementParser',
12071243
];
12081244

1209-
private static $onUnindentParserList = [
1245+
private static $onOutdentParserList = [
12101246
'ListElementParser', // // 順番大事. List -> Definition
12111247
'DefinitionListElementParser',
12121248
'SectionElementParser',
12131249
];
12141250

1215-
private static $onUnchangedIndentParserList = [
1216-
// 'ListElementParser',
1217-
];
1218-
12191251
private static $onEndOfDocumentParserList = [
12201252
'ListElementParser',
12211253
'DefinitionListElementParser',
@@ -1250,12 +1282,12 @@ class Parser {
12501282
// End Parser Configuration ===
12511283

12521284
public static $onResetParserFuncList = [];
1285+
public static $onEmptyLineParserFuncList = [];
1286+
public static $onNewLineParserFuncList = [];
12531287
public static $onPreBeginLineParserFuncList = [];
12541288
public static $onBeginLineParserFuncList = [];
1255-
public static $onEmptyLineParserFuncList = [];
12561289
public static $onIndentParserFuncList = [];
1257-
public static $onUnindentParserFuncList = [];
1258-
public static $onUnchangedIndentParserFuncList = [];
1290+
public static $onOutdentParserFuncList = [];
12591291
public static $onEndOfDocumentParserFuncList = [];
12601292

12611293
private static $blockSeparatorsPattern;
@@ -1311,26 +1343,26 @@ public static function Init() {
13111343
static::$nonVoidHtmlEndTagsPattern = '/' . static::$nonVoidHtmlEndTagsPattern . '/i';
13121344
static::$voidHtmlTagsPattern = '/' . static::$voidHtmlTagsPattern . '/i';
13131345

1314-
foreach (static::$onBeginLineParserList as $parser) {
1315-
static::$onBeginLineParserFuncList[] = ['OutlineText\\' . $parser, 'OnBeginLine'];
1346+
foreach (static::$onEmptyLineParserList as $parser) {
1347+
static::$onEmptyLineParserFuncList[] = ['OutlineText\\' . $parser, 'OnEmptyLine'];
1348+
}
1349+
foreach (static::$onNewLineParserList as $parser) {
1350+
static::$onNewLineParserFuncList[] = ['OutlineText\\' . $parser, 'OnNewLine'];
13161351
}
13171352
foreach (static::$onPreBeginLineParserList as $parser) {
13181353
static::$onPreBeginLineParserFuncList[] = ['OutlineText\\' . $parser, 'OnPreBeginLine'];
13191354
}
1320-
foreach (static::$onEmptyLineParserList as $parser) {
1321-
static::$onEmptyLineParserFuncList[] = ['OutlineText\\' . $parser, 'OnEmptyLine'];
1355+
foreach (static::$onBeginLineParserList as $parser) {
1356+
static::$onBeginLineParserFuncList[] = ['OutlineText\\' . $parser, 'OnBeginLine'];
13221357
}
13231358
foreach (static::$onResetParserList as $parser) {
13241359
static::$onResetParserFuncList[] = ['OutlineText\\' . $parser, 'OnReset'];
13251360
}
13261361
foreach (static::$onIndentParserList as $parser) {
13271362
static::$onIndentParserFuncList[] = ['OutlineText\\' . $parser, 'OnIndent'];
13281363
}
1329-
foreach (static::$onUnindentParserList as $parser) {
1330-
static::$onUnindentParserFuncList[] = ['OutlineText\\' . $parser, 'OnUnindent'];
1331-
}
1332-
foreach (static::$onUnchangedIndentParserList as $parser) {
1333-
static::$onUnchangedIndentParserFuncList[] = ['OutlineText\\' . $parser, 'OnUnchangedIndent'];
1364+
foreach (static::$onOutdentParserList as $parser) {
1365+
static::$onOutdentParserFuncList[] = ['OutlineText\\' . $parser, 'OnOutdent'];
13341366
}
13351367
foreach (static::$onEndOfDocumentParserList as $parser) {
13361368
static::$onEndOfDocumentParserFuncList[] = ['OutlineText\\' . $parser, 'OnEndOfDocument'];
@@ -1405,8 +1437,8 @@ public static function Parse($plainText, &$context = null) {
14051437
continue;
14061438
}
14071439

1408-
// 行頭の前処理
1409-
$output .= static::CallbackEventFuncs(static::$onPreBeginLineParserFuncList, $context);
1440+
// 新しい行が始まった
1441+
$output .= static::CallbackEventFuncs(static::$onNewLineParserFuncList, $context);
14101442

14111443
//
14121444
// --- インデントレベルの変化を見る ----------------------
@@ -1422,20 +1454,15 @@ public static function Parse($plainText, &$context = null) {
14221454
// 左へインデント
14231455
if ($currentChunk["indentLevel"] < $context->indentLevelPrevious) {
14241456
while ($currentChunk["indentLevel"] < $context->indentLevel) {
1425-
$output .= static::CallbackEventFuncs(static::$onUnindentParserFuncList, $context);
1457+
$output .= static::CallbackEventFuncs(static::$onOutdentParserFuncList, $context);
14261458
$context->indentLevel--;
14271459
}
14281460
}
14291461

1430-
// インデントそのまま
1431-
if ($currentChunk["indentLevel"] == $context->indentLevelPrevious) {
1432-
$output .= static::CallbackEventFuncs(static::$onUnchangedIndentParserFuncList, $context);
1433-
}
1434-
14351462
$context->indentLevelPrevious = $context->indentLevel;
14361463

14371464
// End インデントの変化を見る ---
1438-
1465+
14391466
// 空文字の時
14401467
// インデント値はあるが, 空文字
14411468
// その次がインラインコード, html要素のときに起こる.
@@ -1457,14 +1484,17 @@ public static function Parse($plainText, &$context = null) {
14571484
//continue;
14581485
}
14591486

1487+
// 行頭の前処理
1488+
$output .= static::CallbackEventFuncs(static::$onPreBeginLineParserFuncList, $context);
1489+
14601490
// 行頭の処理
14611491
$output .= static::CallbackEventFuncs(static::$onBeginLineParserFuncList, $context);
14621492

14631493
} // End 各チャンクごとに対して ----
14641494

14651495
// すべてのチャンクの処理を終えた場合
14661496
while(0 < $context->indentLevel){
1467-
$output .= static::CallbackEventFuncs(static::$onUnindentParserFuncList, $context);
1497+
$output .= static::CallbackEventFuncs(static::$onOutdentParserFuncList, $context);
14681498
$context->indentLevel--;
14691499
}
14701500
$context->indentLevelPrevious = $context->indentLevel;

0 commit comments

Comments
 (0)