Merge branch 'master' into 499-google-translation
This commit is contained in:
@@ -1,69 +0,0 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
|
||||
"github.com/russross/blackfriday"
|
||||
)
|
||||
|
||||
type renderer struct {
|
||||
*blackfriday.Html
|
||||
}
|
||||
|
||||
func doubleSpace(out *bytes.Buffer) {
|
||||
if out.Len() > 0 {
|
||||
out.WriteByte('\n')
|
||||
}
|
||||
}
|
||||
|
||||
func escapeSingleChar(char byte) (string, bool) {
|
||||
if char == '"' {
|
||||
return """, true
|
||||
}
|
||||
if char == '&' {
|
||||
return "&", true
|
||||
}
|
||||
if char == '<' {
|
||||
return "<", true
|
||||
}
|
||||
if char == '>' {
|
||||
return ">", true
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
func attrEscape(out *bytes.Buffer, src []byte) {
|
||||
org := 0
|
||||
for i, ch := range src {
|
||||
if entity, ok := escapeSingleChar(ch); ok {
|
||||
if i > org {
|
||||
// copy all the normal characters since the last escape
|
||||
out.Write(src[org:i])
|
||||
}
|
||||
org = i + 1
|
||||
out.WriteString(entity)
|
||||
}
|
||||
}
|
||||
if org < len(src) {
|
||||
out.Write(src[org:])
|
||||
}
|
||||
}
|
||||
|
||||
// Using <code> rather than <pre> keeps Google Translate from trying to process it.
|
||||
// BUT it collapses code into one line for some reason, and <pre> preserves newlines.
|
||||
// #TODO Investigating the <pre><code> combo might work.
|
||||
func (*renderer) BlockCode(out *bytes.Buffer, text []byte, info string) {
|
||||
doubleSpace(out)
|
||||
|
||||
endOfLang := strings.IndexAny(info, "\t ")
|
||||
if endOfLang < 0 {
|
||||
endOfLang = len(info)
|
||||
}
|
||||
lang := info[:endOfLang]
|
||||
if len(lang) == 0 || lang == "." {
|
||||
out.WriteString("<pre translate='no'>")
|
||||
}
|
||||
attrEscape(out, text)
|
||||
out.WriteString("</pre>\n")
|
||||
}
|
||||
@@ -58,14 +58,19 @@ func (gw *Gateway) handleTranslation(msg *config.Message, dest *bridge.Bridge, c
|
||||
text = strings.Replace(text, "•", "-", -1)
|
||||
|
||||
// Make sure we use closed <br/> tags
|
||||
const htmlFlags = blackfriday.HTML_USE_XHTML
|
||||
renderer := &renderer{Html: blackfriday.HtmlRenderer(htmlFlags, "", "").(*blackfriday.Html)}
|
||||
const extensions = blackfriday.LINK_TYPE_NOT_AUTOLINK |
|
||||
blackfriday.EXTENSION_HARD_LINE_BREAK |
|
||||
blackfriday.EXTENSION_STRIKETHROUGH |
|
||||
blackfriday.EXTENSION_FENCED_CODE |
|
||||
blackfriday.EXTENSION_HARD_LINE_BREAK
|
||||
output := blackfriday.Markdown([]byte(text), renderer, extensions)
|
||||
const htmlFlags = blackfriday.UseXHTML
|
||||
const extensions = blackfriday.HardLineBreak |
|
||||
blackfriday.Strikethrough |
|
||||
blackfriday.FencedCode
|
||||
renderer := blackfriday.NewHTMLRenderer(blackfriday.HTMLRendererParameters{
|
||||
Flags: htmlFlags,
|
||||
})
|
||||
optList := []blackfriday.Option{
|
||||
blackfriday.WithNoExtensions(),
|
||||
blackfriday.WithExtensions(extensions),
|
||||
blackfriday.WithRenderer(renderer),
|
||||
}
|
||||
output := blackfriday.Run([]byte(text), optList...)
|
||||
text = string(output)
|
||||
flog.Debugf("post-parseMD:"+string(output))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user