Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
CentralNic
parsedown
Commits
6d113f47
Commit
6d113f47
authored
Nov 04, 2013
by
Emanuil Rusev
Browse files
rearrange block types to optimize performance
parent
d4d36127
Changes
3
Hide whitespace changes
Inline
Side-by-side
Parsedown.php
View file @
6d113f47
...
...
@@ -217,56 +217,14 @@ class Parsedown
# Quick Paragraph
if
(
$line
[
0
]
>=
'A'
and
$line
[
'0'
]
!==
'_'
)
if
(
$line
[
0
]
>=
'A'
and
$line
[
0
]
!==
'_'
)
{
goto
paragraph
;
# trust me
}
# Setext Header (---)
if
(
$element
[
'type'
]
===
'p'
and
!
isset
(
$element
[
'interrupted'
])
and
preg_match
(
'/^[-]+[ ]*$/'
,
$line
))
{
$element
[
'type'
]
=
'h.'
;
$element
[
'level'
]
=
2
;
continue
;
}
# Horizontal Rule
if
(
preg_match
(
'/^[ ]{0,3}([-*_])([ ]{0,2}\1){2,}[ ]*$/'
,
$line
))
{
$elements
[]
=
$element
;
$element
=
array
(
'type'
=>
'hr'
,
);
continue
;
}
# List Item
if
(
preg_match
(
'/^([ ]{0,3})(\d+[.]|[*+-])[ ](.*)/'
,
$line
,
$matches
))
{
$elements
[]
=
$element
;
$element
=
array
(
'type'
=>
'li'
,
'ordered'
=>
isset
(
$matches
[
2
][
1
]),
'indentation'
=>
$matches
[
1
],
'last'
=>
true
,
'lines'
=>
array
(
preg_replace
(
'/^[ ]{0,4}/'
,
''
,
$matches
[
3
]),
),
);
continue
;
}
# Code
if
(
preg_match
(
'/^[ ]{4}(.*)/'
,
$line
,
$matches
))
if
(
$line
[
0
]
===
' '
and
preg_match
(
'/^[ ]{4}(.*)/'
,
$line
,
$matches
))
{
if
(
$element
[
'type'
]
===
'code'
)
{
...
...
@@ -279,10 +237,20 @@ class Parsedown
$elements
[]
=
$element
;
$element
=
array
(
'type'
=>
'code'
,
'type'
=>
'code'
,
'text'
=>
$matches
[
1
],
);
}
continue
;
}
# Setext Header (---)
if
(
$line
[
0
]
===
'-'
and
$element
[
'type'
]
===
'p'
and
!
isset
(
$element
[
'interrupted'
])
and
preg_match
(
'/^[-]+[ ]*$/'
,
$line
))
{
$element
[
'type'
]
=
'h.'
;
$element
[
'level'
]
=
2
;
continue
;
}
...
...
@@ -296,17 +264,31 @@ class Parsedown
$level
=
strlen
(
$matches
[
1
]);
$element
=
array
(
'type'
=>
'h.'
,
'text'
=>
$matches
[
2
],
'type'
=>
'h.'
,
'text'
=>
$matches
[
2
],
'level'
=>
$level
,
);
continue
;
}
# Setext Header (===)
if
(
$line
[
0
]
===
'='
and
$element
[
'type'
]
===
'p'
and
!
isset
(
$element
[
'interrupted'
])
and
preg_match
(
'/^[=]+[ ]*$/'
,
$line
))
{
$element
[
'type'
]
=
'h.'
;
$element
[
'level'
]
=
1
;
continue
;
}
# ~
$pure_line
=
ltrim
(
$line
);
# Blockquote
if
(
preg_match
(
'/^
[ ]*
>[ ]?(.*)/'
,
$line
,
$matches
))
if
(
$pure_line
[
0
]
===
'>'
and
preg_match
(
'/^>[ ]?(.*)/'
,
$
pure_
line
,
$matches
))
{
if
(
$element
[
'type'
]
===
'blockquote'
)
{
...
...
@@ -334,45 +316,72 @@ class Parsedown
continue
;
}
#
Setext Header (===)
#
HTML
if
(
$
element
[
'type'
]
===
'p'
and
!
isset
(
$element
[
'interrupted'
])
and
preg_match
(
'/^[=]+[ ]*$/'
,
$line
)
)
if
(
$
pure_line
[
0
]
===
'<'
)
{
$element
[
'type'
]
=
'h.'
;
$element
[
'level'
]
=
1
;
# Block-Level HTML <self-closing/>
if
(
preg_match
(
'{^<.+?/>$}'
,
$pure_line
))
{
$elements
[]
=
$element
;
$element
=
array
(
'type'
=>
''
,
'text'
=>
$pure_line
,
);
continue
;
}
continue
;
}
# Block-Level HTML <open>
# Block-Level HTML <self-closing/>
if
(
preg_match
(
'{^<(\w+)(?:[ ].*?)?>}'
,
$pure_line
,
$matches
))
{
$elements
[]
=
$element
;
$element
=
array
(
'type'
=>
'block'
,
'subtype'
=>
strtolower
(
$matches
[
1
]),
'text'
=>
$pure_line
,
'depth'
=>
0
,
);
preg_match
(
'{</'
.
$matches
[
1
]
.
'>\s*$}'
,
$pure_line
)
and
$element
[
'closed'
]
=
true
;
continue
;
}
}
# Horizontal Rule
if
(
preg_match
(
'
{^<.+?/>$}'
,
$
line
))
if
(
preg_match
(
'
/^([-*_])([ ]{0,2}\1){2,}[ ]*$/'
,
$pure_
line
))
{
$elements
[]
=
$element
;
$element
=
array
(
'type'
=>
''
,
'text'
=>
$line
,
'type'
=>
'hr'
,
);
continue
;
}
#
Block-Level HTML <open>
#
List Item
if
(
preg_match
(
'
{^<(\w+)(?:
[ ].*
?)?>}
'
,
$line
,
$matches
))
if
(
preg_match
(
'
/^([ ]*)(\d+[.]|[*+-])
[ ]
(
.*
)/
'
,
$line
,
$matches
))
{
$elements
[]
=
$element
;
$element
=
array
(
'type'
=>
'block'
,
'subtype'
=>
strtolower
(
$matches
[
1
]),
'text'
=>
$line
,
'depth'
=>
0
,
'type'
=>
'li'
,
'ordered'
=>
isset
(
$matches
[
2
][
1
]),
'indentation'
=>
$matches
[
1
],
'last'
=>
true
,
'lines'
=>
array
(
preg_replace
(
'/^[ ]{0,4}/'
,
''
,
$matches
[
3
]),
),
);
preg_match
(
'{</'
.
$matches
[
1
]
.
'>\s*$}'
,
$line
)
and
$element
[
'closed'
]
=
true
;
continue
;
}
...
...
tests/data/sparse_list.html
View file @
6d113f47
<p>
Here's a list where items are separated by empty lines:
</p>
<ul>
<li>
<p>
list item
</p>
</li>
<li>
another list item
</li>
</ul>
<p>
Here's an ordered one:
</p>
<ol>
<li>
<p>
item one
</p>
</li>
<li>
item two
</li>
</ol>
\ No newline at end of file
<p>
Here's a sparse list:
</p>
<ul>
<li>
<p>
list item
</p>
</li>
<li>
another list item
</li>
</ul>
<p>
Here's one with an indented list item:
</p>
<ul>
<li>
<p>
li
</p>
<ul>
<li>
li
</li>
</ul>
</li>
</ul>
\ No newline at end of file
tests/data/sparse_list.md
View file @
6d113f47
Here's a list where items are separated by empty lines:
-
list item
-
another list item
Here's an ordered one:
1.
item one
2.
item two
\ No newline at end of file
Here's a sparse list:
-
list item
-
another list item
Here's one with an indented list item:
-
li
-
li
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment