Reputation: 48973
I am working on a PHP script that takes a plain text file task list and parses things like:
- task name
<span class="tag">@tag-name</span>
<span class="tag done">@done</span>
This PHP function below is what does the replacements...
function get_marked_up_todo($todo){
$todo = htmlspecialchars($todo,ENT_QUOTES)."\n\n";
$search = array('/(.+:)(.+)\n\n/sU', // Get projects
'/(- ([^\@\n]+).+)/', // Get todos
'/(.+:)/', // Get headings
'/\n([^\<\n].+)/', // Get notes
'/- (.+@done)/', // Get done
'/(@due\([^\)]+\))/', // Get due tags
'/(@(?!due)[^\s]+)/', // Get tags
"/\t/",
'/`(.*?)`/', // inline code
);
$replace = array("<div class=\"project\">\n$1$2\n</div>\n\n",
'<span class="todo"><input type="checkbox" value="'.trim('\2').'"> \1</span>',
'<h1>\1</h1>',
"\n\t<span class=\"note\">$1</span>",
'<span class="bullet-done">? ? ??</span> - <strike>\1</strike>',
'<span class="tag due">\1</span>',
'<span class="tag">\1</span>',
"\t<span class=\"tab\"></span>",
'<code>\1</code>',
);
return preg_replace($search, $replace, $todo);
}
In the above search and replace arrays the very last item in both arrays is a new pattern I added to find inline code wrapped in backticks like Markdown inline code.
The problem is, on the output each task item row gets a checkbox input field added to the front of the task row and in the value of this checkbox my code is being parsed.
All the other replacement RegEx does not show up in the checkbox values only when I add an item like this inline code or any other item I have added such as RegEx for bold text and italic.
Why does mine show up in the checkbox value HTML and none of the others do?
I have setup a demo to show the output here of the PHP - https://3v4l.org/f0e8W#output
Here is the full code as well
<?php
echo "<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>TODO.todo</title></head>
<body>
<style>
.project {
line-height: 4px;
}
.bullet-done {
font-weight: bold;
font-style: normal;
color: rgba(0,114,62,1.0);
}
.note{
display: block;
color: rgba(133,130,102,1.0);
font-weight: normal;
font-style: normal;
}
.todo {
display: inline-block;
}
.tag {
font-weight: bold;
font-style: normal;
color: rgba(160,46,43,0.6);
}
body {
background: rgba(239,233,183,1.0);
color: rgba(0,0,0,0.31);
font-weight: normal;
font-style: normal;
}
h1 {
font-weight: bold;
font-style: normal;
background: rgba(0,0,0,0.06);
color: rgba(188,100,74,1.0);
width: 100%;
line-height: 34px;
}
.tab{
display: inline-block;
width:0px;
height: 0px;
background: #000000;
</style><pre>";
$todo = 'Version 1:
This file is in TaskPaper format.
Tabs are used to indent.
Each task begins with a "- ".
Projects end with a ":".
Tags are in the format "@tag_name".
All other lines (such as these) are considered as notes,
and are to be ignored.
- User signup
- Register for an account
- Log in @done
- Forget password
- Manage users
- Create users @in_progress
- Delete users
- User profile page @40%
- Blog
- Creating new posts @done
- Comments @done
- Moderating comments @done
This is my todo list:
This is a note about the list.
- this is an item @done
- and this is @me
this is a note about my done item
- this is the last @java @shopping @item @done
This is a second list:
- Add more funk to something @work @java
- Send something somewhere @work @email @due(12 Aug 07)
- this is an example
- dfgdfg
ggg
hfghf
- hgh
- dfygdfgdf
List:
- gdfgdf `inline code` hhf
- gdfgdf
- dfgdfg @done
';
echo get_marked_up_todo($todo);
echo '</pre></body></html>';
function get_marked_up_todo($todo){
$todo = htmlspecialchars($todo,ENT_QUOTES)."\n\n";
$search = array('/(.+:)(.+)\n\n/sU', // Get projects
'/(- ([^\@\n]+).+)/', // Get todos
'/(.+:)/', // Get headings
'/\n([^\<\n].+)/', // Get notes
'/- (.+@done)/', // Get done
'/(@due\([^\)]+\))/', // Get due tags
'/(@(?!due)[^\s]+)/', // Get tags
"/\t/",
'/`(.*?)`/', // inline code
);
$replace = array("<div class=\"project\">\n$1$2\n</div>\n\n",
'<span class="todo"><input type="checkbox" value="'.trim('\2').'"> \1</span>',
'<h1>\1</h1>',
"\n\t<span class=\"note\">$1</span>",
'<span class="bullet-done">? ? ??</span> - <strike>\1</strike>',
'<span class="tag due">\1</span>',
'<span class="tag">\1</span>',
"\t<span class=\"tab\"></span>",
'<code>\1</code>',
);
return preg_replace($search, $replace, $todo);
}
Upvotes: 1
Views: 52
Reputation: 775
If I understood the issue correctly, your issue is that when you use your own replacement for <code>
, the <input>
tag looks like this:
<input type="checkbox" value="gdfgdf <code>inline code</code> hh">
but you want to it to NOT include the <code>...</code>
part, like this:
<input type="checkbox" value="gdfgdf ">
.
If my understanding is correct, then you fix you simply need to fix the RegExp that's in charge of rendering the <input>
tag, which is this one:
'/(- ([^\@\n]+).+)/', // Get todos
The way it works is by taking everything from -
until either a @
or a newline (\n
) shows up. You want to add do it the backtick:
'/(- ([^\@\n]+).+)/', // Get todos
This will make the RegExp stop capturing when it encounters the first ` and will fix your issue (again, if I understood it correctly).
Upvotes: 1