formatting.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2018 Tulir Asokan
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package main
  17. import (
  18. "fmt"
  19. "regexp"
  20. "strings"
  21. "maunium.net/go/gomatrix/format"
  22. "maunium.net/go/mautrix-whatsapp/whatsapp-ext"
  23. )
  24. func (user *User) newHTMLParser() *format.HTMLParser {
  25. return &format.HTMLParser{
  26. TabsToSpaces: 4,
  27. Newline: "\n",
  28. PillConverter: func(mxid, eventID string) string {
  29. if mxid[0] == '@' {
  30. puppet := user.GetPuppetByMXID(mxid)
  31. fmt.Println(mxid, puppet)
  32. if puppet != nil {
  33. return "@" + puppet.PhoneNumber()
  34. }
  35. }
  36. return mxid
  37. },
  38. BoldConverter: func(text string) string {
  39. return fmt.Sprintf("*%s*", text)
  40. },
  41. ItalicConverter: func(text string) string {
  42. return fmt.Sprintf("_%s_", text)
  43. },
  44. StrikethroughConverter: func(text string) string {
  45. return fmt.Sprintf("~%s~", text)
  46. },
  47. MonospaceConverter: func(text string) string {
  48. return fmt.Sprintf("```%s```", text)
  49. },
  50. MonospaceBlockConverter: func(text string) string {
  51. return fmt.Sprintf("```%s```", text)
  52. },
  53. }
  54. }
  55. var italicRegex = regexp.MustCompile("([\\s>~*]|^)_(.+?)_([^a-zA-Z\\d]|$)")
  56. var boldRegex = regexp.MustCompile("([\\s>_~]|^)\\*(.+?)\\*([^a-zA-Z\\d]|$)")
  57. var strikethroughRegex = regexp.MustCompile("([\\s>_*]|^)~(.+?)~([^a-zA-Z\\d]|$)")
  58. var codeBlockRegex = regexp.MustCompile("```(?:.|\n)+?```")
  59. var mentionRegex = regexp.MustCompile("@[0-9]+")
  60. func (user *User) newWhatsAppFormatMaps() (map[*regexp.Regexp]string, map[*regexp.Regexp]func(string) string) {
  61. return map[*regexp.Regexp]string{
  62. italicRegex: "$1<em>$2</em>$3",
  63. boldRegex: "$1<strong>$2</strong>$3",
  64. strikethroughRegex: "$1<del>$2</del>$3",
  65. }, map[*regexp.Regexp]func(string) string{
  66. codeBlockRegex: func(str string) string {
  67. str = str[3 : len(str)-3]
  68. if strings.ContainsRune(str, '\n') {
  69. return fmt.Sprintf("<pre><code>%s</code></pre>", str)
  70. }
  71. return fmt.Sprintf("<code>%s</code>", str)
  72. },
  73. mentionRegex: func(str string) string {
  74. jid := str[1:] + whatsappExt.NewUserSuffix
  75. puppet := user.GetPuppetByJID(jid)
  76. return fmt.Sprintf(`<a href="https://matrix.to/#/%s">%s</a>`, puppet.MXID, puppet.Displayname)
  77. },
  78. }
  79. }