3 ms·
The underlying problem with the first, simple, approach is that the template it is using to get things from git, "<tr> <td>%H</td> <td>%s</td> <tr>" interpo
by tzs 9y ago
The underlying problem with the first, simple, approach is that the template it is using to get things from git,
"<tr> <td>%H</td> <td>%s</td> <tr>"
interpolates values that need to be escaped, but includes literal text that must not be escaped. (My guess is that the author meant "</tr>" for the last element, but the article says "<tr>" so I'm going with that).
The author's approach to deal with that is to mark the places in the template where escaping will be needed, and then make and use an escaping tool that recognizes those marks and just escapes the marked segments.
A simpler approach is to eliminate the underlying problem. For getting the data out of git use a template where the literal text is safe to escape, such as this:
"%H,%s"
The escaping can then be done by a tool that escapes its entire input. That will leave the comma from the template alone, and will not introduce any new commas. The interpolation of %s might have introduced commas, but they will all be after the literal comma from the template. The interpolation of %H will not introduce commas.
The output from the escaper can then be transformed into the final output by replacing the first "," with "</td> <td>", prepending "<tr> <td>", and appending "</td> <tr>". All of these are simple in a shell pipeline using sed.
- chubot 9y agoSeveral people brought up alternative solutions like this, and I addressed them: https://news.ycombinator.com/item?id=15295556 https://news.ycombinator.com/item?id=15295556 Summary: I may have oversimplified the problem in my example. I think my solution is nicer for the real problem. It has fewer assumptions and will "scale up" to more fields with arbitrary text. I want to write the escaping ONCE, not modify it every time I change the format of the output table.