From 04567c765e92ad60c685c1b2fe7e77c46e065645 Mon Sep 17 00:00:00 2001 From: Wim Date: Sun, 6 Jan 2019 22:25:19 +0100 Subject: Add support for markdown to HTML conversion (matrix). Closes #663 (#670) This uses our own gomatrix lib with the SendHTML function which adds HTML to formatted_body in matrix. golang-commonmark is used to convert markdown into valid HTML. --- .../golang-commonmark/puny/.gitlab-ci.yml | 16 ++ vendor/gitlab.com/golang-commonmark/puny/LICENSE | 10 + vendor/gitlab.com/golang-commonmark/puny/README.md | 8 + vendor/gitlab.com/golang-commonmark/puny/puny.go | 287 +++++++++++++++++++++ 4 files changed, 321 insertions(+) create mode 100644 vendor/gitlab.com/golang-commonmark/puny/.gitlab-ci.yml create mode 100644 vendor/gitlab.com/golang-commonmark/puny/LICENSE create mode 100644 vendor/gitlab.com/golang-commonmark/puny/README.md create mode 100644 vendor/gitlab.com/golang-commonmark/puny/puny.go (limited to 'vendor/gitlab.com/golang-commonmark/puny') diff --git a/vendor/gitlab.com/golang-commonmark/puny/.gitlab-ci.yml b/vendor/gitlab.com/golang-commonmark/puny/.gitlab-ci.yml new file mode 100644 index 00000000..aaf2cbdd --- /dev/null +++ b/vendor/gitlab.com/golang-commonmark/puny/.gitlab-ci.yml @@ -0,0 +1,16 @@ +image: golang:1.11 + +stages: + - build + - test + +build: + stage: build + script: + - go build ./... + +test: + stage: test + script: + - test -z "$(gofmt -l . | tee /dev/stderr)" + - go test ./... diff --git a/vendor/gitlab.com/golang-commonmark/puny/LICENSE b/vendor/gitlab.com/golang-commonmark/puny/LICENSE new file mode 100644 index 00000000..9cdcd201 --- /dev/null +++ b/vendor/gitlab.com/golang-commonmark/puny/LICENSE @@ -0,0 +1,10 @@ +Copyright (c) 2015, The Authors +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/vendor/gitlab.com/golang-commonmark/puny/README.md b/vendor/gitlab.com/golang-commonmark/puny/README.md new file mode 100644 index 00000000..1496b554 --- /dev/null +++ b/vendor/gitlab.com/golang-commonmark/puny/README.md @@ -0,0 +1,8 @@ +puny [![License](https://img.shields.io/badge/licence-BSD--2--Clause-blue.svg)](https://opensource.org/licenses/BSD-2-Clause) [![GoDoc](http://godoc.org/gitlab.com/golang-commonmark/puny?status.svg)](http://godoc.org/gitlab.com/golang-commonmark/puny) [![Pipeline status](https://gitlab.com/golang-commonmark/puny/badges/master/pipeline.svg)](https://gitlab.com/golang-commonmark/puny/commits/master) +==== + +Package puny provides functions for encoding/decoding to/from punycode. + +## Install + + go get -u gitlab.com/golang-commonmark/puny diff --git a/vendor/gitlab.com/golang-commonmark/puny/puny.go b/vendor/gitlab.com/golang-commonmark/puny/puny.go new file mode 100644 index 00000000..8f71402c --- /dev/null +++ b/vendor/gitlab.com/golang-commonmark/puny/puny.go @@ -0,0 +1,287 @@ +// Copyright 2015 The Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package puny provides functions for encoding/decoding to/from punycode. +package puny + +import ( + "errors" + "strings" + "unicode/utf8" +) + +const ( + maxInt32 int32 = 2147483647 + base int32 = 36 + tMin int32 = 1 + baseMinusTMin = base - tMin + tMax int32 = 26 + skew int32 = 38 + damp int32 = 700 + initialBias int32 = 72 + initialN int32 = 128 +) + +var ( + ErrOverflow = errors.New("overflow: input needs wider integers to process") + ErrNotBasic = errors.New("illegal input >= 0x80 (not a basic code point)") + ErrInvalidInput = errors.New("invalid input") +) + +func adapt(delta, numPoints int32, firstTime bool) int32 { + if firstTime { + delta /= damp + } else { + delta /= 2 + } + delta += delta / numPoints + k := int32(0) + for delta > baseMinusTMin*tMax/2 { + delta = delta / baseMinusTMin + k += base + } + return k + (baseMinusTMin+1)*delta/(delta+skew) +} + +func basicToDigit(b byte) int32 { + switch { + case b >= '0' && b <= '9': + return int32(b - 22) + case b >= 'A' && b <= 'Z': + return int32(b - 'A') + case b >= 'a' && b <= 'z': + return int32(b - 'a') + } + return base +} + +func digitToBasic(digit int32) byte { + switch { + case digit >= 0 && digit <= 25: + return byte(digit) + 'a' + case digit >= 26 && digit <= 35: + return byte(digit) - 26 + '0' + } + panic("unreachable") +} + +func lastIndex(s string, c byte) int { + for i := len(s) - 1; i >= 0; i-- { + if s[i] == c { + return i + } + } + return -1 +} + +func ascii(s string) bool { + for _, r := range s { + if r > 0x7e { + return false + } + } + return true +} + +// Decode converts a Punycode string of ASCII-only symbols to a string of Unicode symbols. +func Decode(s string) (string, error) { + basic := lastIndex(s, '-') + output := make([]rune, 0, len(s)) + for i := 0; i < basic; i++ { + b := s[i] + if b >= 0x80 { + return "", ErrNotBasic + } + output = append(output, rune(b)) + } + + i, n, bias, pos := int32(0), initialN, initialBias, basic+1 + + for pos < len(s) { + oldi, w, k := i, int32(1), base + for { + digit := basicToDigit(s[pos]) + pos++ + + if digit >= base || digit > (maxInt32-i)/w { + return "", ErrOverflow + } + + i += digit * w + + t := k - bias + if t < tMin { + t = tMin + } else if t > tMax { + t = tMax + } + + if digit < t { + break + } + + if pos == len(s) { + return "", ErrInvalidInput + } + + baseMinusT := base - t + if w > maxInt32/baseMinusT { + return "", ErrOverflow + } + + w *= baseMinusT + k += base + } + + out := int32(len(output) + 1) + bias = adapt(i-oldi, out, oldi == 0) + + if i/out > maxInt32-n { + return "", ErrOverflow + } + + n += i / out + i %= out + + output = append(output, 0) + copy(output[i+1:], output[i:]) + output[i] = rune(n) + + i++ + } + + return string(output), nil +} + +// Encode converts a string of Unicode symbols (e.g. a domain name label) to a +// Punycode string of ASCII-only symbols. +func Encode(input string) (string, error) { + n := initialN + delta := int32(0) + bias := initialBias + + var output []byte + runes := 0 + for _, r := range input { + if r >= 0x80 { + runes++ + continue + } + output = append(output, byte(r)) + } + + basicLength := len(output) + handledCPCount := basicLength + + if basicLength > 0 { + output = append(output, '-') + } + + for runes > 0 { + m := maxInt32 + for _, r := range input { + if r >= n && r < m { + m = r + } + } + + handledCPCountPlusOne := int32(handledCPCount + 1) + if m-n > (maxInt32-delta)/handledCPCountPlusOne { + return "", ErrOverflow + } + + delta += (m - n) * handledCPCountPlusOne + n = m + + for _, r := range input { + if r < n { + delta++ + if delta < 0 { + return "", ErrOverflow + } + continue + } + if r > n { + continue + } + q := delta + for k := base; ; k += base { + t := k - bias + if t < tMin { + t = tMin + } else if t > tMax { + t = tMax + } + if q < t { + break + } + qMinusT := q - t + baseMinusT := base - t + output = append(output, digitToBasic(t+qMinusT%baseMinusT)) + q = qMinusT / baseMinusT + } + + output = append(output, digitToBasic(q)) + bias = adapt(delta, handledCPCountPlusOne, handledCPCount == basicLength) + delta = 0 + handledCPCount++ + runes-- + } + delta++ + n++ + } + return string(output), nil +} + +func sep(r rune) bool { return r == '.' || r == '。' || r == '.' || r == '。' } + +func mapLabels(s string, fn func(string) string) string { + var result string + i := strings.IndexByte(s, '@') + if i != -1 { + result = s[:i+1] + s = s[i+1:] + } + var labels []string + start := 0 + for i, r := range s { + if !sep(r) { + continue + } + labels = append(labels, fn(s[start:i])) + start = i + utf8.RuneLen(r) + } + labels = append(labels, fn(s[start:])) + return result + strings.Join(labels, ".") +} + +// ToUnicode converts a Punycode string representing a domain name or an email address +// to Unicode. Only the Punycoded parts of the input will be converted. +func ToUnicode(s string) string { + return mapLabels(s, func(s string) string { + if !strings.HasPrefix(s, "xn--") { + return s + } + d, err := Decode(strings.ToLower(s[4:])) + if err != nil { + return s + } + return d + }) +} + +// ToASCII converts a Unicode string representing a domain name or an email address to +// Punycode. Only the non-ASCII parts of the domain name will be converted. +func ToASCII(s string) string { + return mapLabels(s, func(s string) string { + if ascii(s) { + return s + } + d, err := Encode(s) + if err != nil { + return s + } + return "xn--" + d + }) +} -- cgit v1.2.3