Commit 9bf763be authored by Matthias Simon's avatar Matthias Simon
Browse files

Add initial tool, update Makefile

parent 05b9286d
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line

.PHONY: all ## generate everything
all:

.PHONY: install ## build and install tools
install:
	cd tools/etsi && go get && go install

LANGUAGE_SOURCES := $(wildcard src/*.md)
TARGET_LANGUAGE_MD := build/language.md
$(TARGET_LANGUAGE_MD): $(LANGUAGE_SOURCES)
	@mkdir build
	cat $(LANGUAGE_SOURCES) > "$@"


TARGET_EBNF := assets/ttcn3.ebnf
$(TARGET_EBNF): $(TARGET_LANGUAGE_MD)



# It's good practice to provide a help target describing available targets.
# This script greps through the Makefile and displays all phony-targets having a

tools/etsi/go.mod

0 → 100644
+15 −0
Original line number Diff line number Diff line
module etsi

go 1.26.2

require (
	github.com/spf13/cobra v1.8.0
	github.com/yuin/goldmark v1.7.8
	golang.org/x/text v0.22.0
	golang.org/x/tools v0.30.0
)

require (
	github.com/inconshreveable/mousetrap v1.1.0 // indirect
	github.com/spf13/pflag v1.0.5 // indirect
)

tools/etsi/go.sum

0 → 100644
+16 −0
Original line number Diff line number Diff line
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/yuin/goldmark v1.7.8 h1:iERMLn0/QJeHFhxSt3p6PeN9mGnvIKSpG9YYorDMnic=
github.com/yuin/goldmark v1.7.8/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
golang.org/x/tools v0.30.0 h1:BgcpHewrV5AUp2G9MebG4XPFI1E2W41zU1SaqVA9vJY=
golang.org/x/tools v0.30.0/go.mod h1:c347cR/OJfw5TI+GfX7RUPNMdDRRbjvYTS0jPyvsVtY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+29 −0
Original line number Diff line number Diff line
This copy of https://github.com/rsc/markdown/ has following license:

Copyright (c) 2009 The Go 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:

   * Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
   * 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.
   * Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

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
OWNER 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.
+220 −0
Original line number Diff line number Diff line
// Copyright 2023 The Go 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 markdown

import (
	"fmt"
	"strings"
	"testing"
)

var rep = strings.Repeat

func repf(f func(int) string, n int) string {
	out := make([]string, n)
	for i := 0; i < n; i++ {
		out[i] = f(i)
	}
	return strings.Join(out, "")
}

// Many cases here derived from cmark-gfm/test/pathological_tests.py

var bigTests = []struct {
	name string
	in   string
	out  string
}{
	{
		"nested strong emph",
		rep("*a **a ", 65000) + "b" + rep(" a** a*", 65000),
		"<p>" + rep("<em>a <strong>a ", 65000) + "b" + rep(" a</strong> a</em>", 65000) + "</p>\n",
	},
	{
		"many emph closers with no openers",
		rep("a_ ", 65000),
		"",
	},
	{
		"many emph openers with no closers",
		rep("_a ", 65000),
		"",
	},
	{
		"many link closers with no openers",
		rep("a]", 65000),
		"",
	},
	{
		"many link openers with no closers",
		rep("[a", 65000),
		"",
	},
	{
		"mismatched openers and closers",
		rep("*a_ ", 50000),
		"",
	},
	{
		"openers and closers multiple of 3",
		"a**b" + rep("c* ", 50000),
		"",
	},
	{
		"link openers and emph closers",
		rep("[ a_", 50000),
		"",
	},
	{
		"pattern [ (]( repeated",
		rep("[ (](", 80000),
		"",
	},
	{
		"pattern ![[]() repeated",
		rep("![[]()", 160000),
		"<p>" + rep(`![<a href=""></a>`, 160000) + "</p>\n",
	},
	{
		"hard link/emph case",
		"**x [a*b**c*](d)",
		`<p>**x <a href="d">a<em>b**c</em></a></p>` + "\n",
	},
	{
		"nested brackets",
		rep("[", 50000) + "a" + rep("]", 50000),
		"",
	},
	{
		"nested block quotes",
		rep("> ", 50000) + "a",
		rep("<blockquote>\n", 50000) + "<p>a</p>\n" + rep("</blockquote>\n", 50000),
	},
	{
		"deeply nested lists",
		repf(func(x int) string { return rep("  ", x) + "* a\n" }, 4000),
		"<ul>\n" + rep("<li>a\n<ul>\n", 4000-1) + "<li>a</li>\n" + rep("</ul>\n</li>\n", 4000-1) + "</ul>\n",
	},
	{
		"backticks",
		repf(func(x int) string { return "e" + rep("`", x) }, 5000),
		"",
	},
	{
		"backticks2",
		repf(func(x int) string { return "e" + rep("`", 5000-x) }, 5000),
		"",
	},
	{
		"unclosed links A",
		rep("[a](<b", 30000),
		"<p>" + rep("[a](&lt;b", 30000) + "</p>\n",
	},
	{
		"unclosed links B",
		rep("[a](b", 30000),
		"",
	},
	{
		"unclosed links C",
		rep("[a](b\\#", 30000),
		"<p>" + rep("[a](b#", 30000) + "</p>\n",
	},
	{
		"unclosed <!--",
		"</" + rep(" <!--", 30000),
		"<p>&lt;/" + rep(" &lt;!--", 30000) + "</p>\n",
	},
	{
		"unclosed <?",
		"</" + rep(" <?", 30000),
		"<p>&lt;/" + rep(" &lt;?", 30000) + "</p>\n",
	},
	{
		"unclosed <!X",
		"</" + rep(" <!X", 30000),
		"<p>&lt;/" + rep(" &lt;!X", 30000) + "</p>\n",
	},
	{
		"unclosed <![CDATA[",
		"</" + rep(" <![CDATA[", 30000),
		"<p>&lt;/" + rep(" &lt;![CDATA[", 30000) + "</p>\n",
	},
	{
		"tables",
		rep("abc\ndef\n|-\n", 30000),
		"<p>abc</p>\n<table>\n<thead>\n<tr>\n<th>def</th>\n</tr>\n</thead>\n<tbody>\n" +
			rep("<tr>\n<td>abc</td>\n</tr>\n<tr>\n<td>def</td>\n</tr>\n<tr>\n<td>-</td>\n</tr>\n", 30000-1) +
			"</tbody>\n</table>\n",
	},
}

func compress(s string) string {
	var out []byte
	start := 0
S:
	for i := 0; i+4 < len(s); i++ {
		c := s[i]
		for j := i + 1; j < i+100 && j < len(s); j++ {
			if s[j] == c {
				n := 1
				w := j - i
				for j+w <= len(s) && s[i:i+w] == s[j:j+w] {
					j += w
					n++
				}
				if n > 2 {
					out = append(out, s[start:i]...)
					out = fmt.Appendf(out, "«%d:%s»", n, s[i:i+w])
					start = j
					i = start - 1
					continue S
				}
			}
		}
	}
	out = append(out, s[start:]...)
	return string(out)
}

func TestBig(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping in -short mode")
	}
	for _, tt := range bigTests {
		t.Run(tt.name, func(t *testing.T) {
			var p Parser
			p.Table = true
			doc := p.Parse(tt.in)
			out := ToHTML(doc)
			if tt.out == "" {
				tt.out = "<p>" + strings.TrimSpace(tt.in) + "</p>\n"
			}
			if out != tt.out {
				t.Fatalf("%s: ToHTML(%q):\nhave %q\nwant %q", tt.name, compress(tt.in), compress(out), compress(tt.out))
			}
		})
	}
}

func bench(b *testing.B, text string) {
	for i := 0; i < b.N; i++ {
		var p Parser
		_ = ToHTML(p.Parse(text))
	}
	b.SetBytes(int64(len(text)))
}

func BenchmarkBrackets(b *testing.B) {
	bench(b, rep("[", 10000)+"a"+rep("]", 10000))
}

func BenchmarkDeepList(b *testing.B) {
	bench(b, repf(func(x int) string { return rep("  ", x) + "* a\n" }, 1000))
}

func BenchmarkList(b *testing.B) {
	bench(b, repf(func(x int) string { return "* a\n" }, 1000))
}
Loading