stmt/html: switch to backtick-delimited string for large multiline cases

This commit is contained in:
mappu 2020-04-05 17:46:25 +12:00
parent 68b554a6ec
commit cdeea4c09c
2 changed files with 21 additions and 8 deletions

View File

@ -4,11 +4,16 @@ $header = "bar";
?>
<!DOCTYPE html>
<html>
<h1><?=$header?></h1>
<ul>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<h1><?=$header?></h1>
<ul>
<?php for($i = 0; $i < 3; ++$i) { ?>
<li><?=$i?></li>
<li><?=$i?></li>
<?php } ?>
</ul>
</ul>
</body>
</html>

14
node.go
View File

@ -379,9 +379,17 @@ func convert(n_ node.Node) (string, error) {
case *stmt.InlineHtml:
// Convert into fmt.Print
// TODO the result from strconv.Quote is not that nice to maintain if there are multiple newlines
// Should convert it into a backtick-delimeted multiline string instead
return "fmt.Print(" + strconv.Quote(n.Value) + ")\n", nil // newline - standalone statement
var quoted string
if !strings.Contains(n.Value, "`") && strings.Count(n.Value, "\n") >= 3 { // TODO make the heuristic configurable
// Use backtick-delimited multiline string
quoted = "`" + n.Value + "`"
} else {
// Can't trivially represent it with backticks, or it's not multiline "enough" to bother - use full Go quoting
quoted = strconv.Quote(n.Value)
}
return "fmt.Print(" + quoted + ")\n", nil // newline - standalone statement
case *stmt.Nop:
return "", nil