admin管理员组文章数量:1026620
I'm considering supporting both Textile and Markdown on a current project. I would prefer not forcing users to choose one or the other. Is there a way to auto-detect which the user is using? How would you go about this? I'd like to find / develop both a JavaScript and a PHP solution so I can provide live previews as well as process the user input on the server-side.
I'm considering supporting both Textile and Markdown on a current project. I would prefer not forcing users to choose one or the other. Is there a way to auto-detect which the user is using? How would you go about this? I'd like to find / develop both a JavaScript and a PHP solution so I can provide live previews as well as process the user input on the server-side.
Share Improve this question asked Sep 26, 2008 at 7:38 Andrew HedgesAndrew Hedges 21.8k16 gold badges70 silver badges79 bronze badges3 Answers
Reset to default 7Consider that users might only use one specific syntax element in a posting, so you'd have to check for everything. Looking for "h1." obviously only works if the user uses exactly that element.
It's pretty easy with things like headers, but consider that markdown formats *this*
as <em>this</em>
and Textile will convert that to <strong>this</strong>
instead. So you'd have ambiguous syntax constructs that would yield different results in each language.
I'd suggest going with a user choice. Try to find out what syntax is generally preferred by your users (or you), offer an "use x instead of y" checkbox for those who want the other choice.
This really shouldn't be that hard. Markdown does not support the following syntax;
h1. Header
p. Paragraph
... so you simply scan for that to check if it is textile. Very simple regular expression to get you started (scans for lines beginning with hX. or p.) in PHP code:
if (preg_match('/^(p|h[1-6])\. /m', $subject))
{
// Successful match
} else
{
// Match attempt failed
}
You will probably be able to write your own regex to scan for Markdown.
Auto-detection, I don't know, both are based on "natural" typing.
Perhaps you can ask the user to choose a format, with a pair of radio-buttons or something.
I'm considering supporting both Textile and Markdown on a current project. I would prefer not forcing users to choose one or the other. Is there a way to auto-detect which the user is using? How would you go about this? I'd like to find / develop both a JavaScript and a PHP solution so I can provide live previews as well as process the user input on the server-side.
I'm considering supporting both Textile and Markdown on a current project. I would prefer not forcing users to choose one or the other. Is there a way to auto-detect which the user is using? How would you go about this? I'd like to find / develop both a JavaScript and a PHP solution so I can provide live previews as well as process the user input on the server-side.
Share Improve this question asked Sep 26, 2008 at 7:38 Andrew HedgesAndrew Hedges 21.8k16 gold badges70 silver badges79 bronze badges3 Answers
Reset to default 7Consider that users might only use one specific syntax element in a posting, so you'd have to check for everything. Looking for "h1." obviously only works if the user uses exactly that element.
It's pretty easy with things like headers, but consider that markdown formats *this*
as <em>this</em>
and Textile will convert that to <strong>this</strong>
instead. So you'd have ambiguous syntax constructs that would yield different results in each language.
I'd suggest going with a user choice. Try to find out what syntax is generally preferred by your users (or you), offer an "use x instead of y" checkbox for those who want the other choice.
This really shouldn't be that hard. Markdown does not support the following syntax;
h1. Header
p. Paragraph
... so you simply scan for that to check if it is textile. Very simple regular expression to get you started (scans for lines beginning with hX. or p.) in PHP code:
if (preg_match('/^(p|h[1-6])\. /m', $subject))
{
// Successful match
} else
{
// Match attempt failed
}
You will probably be able to write your own regex to scan for Markdown.
Auto-detection, I don't know, both are based on "natural" typing.
Perhaps you can ask the user to choose a format, with a pair of radio-buttons or something.
本文标签: phpHow would you go about autodetecting Textile versus MarkdownStack Overflow
版权声明:本文标题:php - How would you go about auto-detecting Textile versus Markdown? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745648635a2161177.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论