From fed55bb1221c6c35d3a2cfe8c475fc5895c06acd Mon Sep 17 00:00:00 2001 From: Wim Date: Tue, 31 May 2022 20:14:32 +0200 Subject: [PATCH] replace new line with
in markdown tables --- examples/doc/example.docbook | 2 +- examples/doc/example.html | 4 +++- examples/doc/example.json | 2 +- examples/doc/example.md | 12 ++++++------ examples/doc/example.txt | 4 +++- examples/proto/Vehicle.proto | 6 +++++- filters.go | 14 +++++++++++++- filters_test.go | 14 ++++++++++++++ renderer.go | 1 + 9 files changed, 47 insertions(+), 12 deletions(-) diff --git a/examples/doc/example.docbook b/examples/doc/example.docbook index dbf34ace..f45942b7 100644 --- a/examples/doc/example.docbook +++ b/examples/doc/example.docbook @@ -463,7 +463,7 @@ model_name string required - The car model name, e.g. "Z3". + model_name represents the car model name.This field typically contains the manufacturer name, e.g. "Porsche 911". diff --git a/examples/doc/example.html b/examples/doc/example.html index 96e463bb..ca559375 100644 --- a/examples/doc/example.html +++ b/examples/doc/example.html @@ -721,7 +721,9 @@

Model

model_name string required -

The car model name, e.g. "Z3".

+

model_name represents the car model name. + +This field typically contains the manufacturer name, e.g. "Porsche 911".

diff --git a/examples/doc/example.json b/examples/doc/example.json index 08486ccd..29611017 100644 --- a/examples/doc/example.json +++ b/examples/doc/example.json @@ -564,7 +564,7 @@ }, { "name": "model_name", - "description": "The car model name, e.g. \"Z3\".", + "description": "model_name represents the car model name.\n\nThis field typically contains the manufacturer name, e.g. \"Porsche 911\".", "label": "required", "type": "string", "longType": "string", diff --git a/examples/doc/example.md b/examples/doc/example.md index 15b5bf35..cc73bca8 100644 --- a/examples/doc/example.md +++ b/examples/doc/example.md @@ -70,7 +70,7 @@ Represents the status of a vehicle booking. | Field | Type | Label | Description | | ----- | ---- | ----- | ----------- | | id | [int32](#int32) | | Unique booking status ID. | -| description | [string](#string) | | Booking status description. E.g. "Active". | +| description | [string](#string) | | Booking status description. E.g. "Active". | @@ -195,7 +195,7 @@ Represents a manufacturer of cars. | Field | Type | Label | Description | | ----- | ---- | ----- | ----------- | | id | [int32](#int32) | required | The unique manufacturer ID. | -| code | [string](#string) | required | A manufacturer code, e.g. "DKL4P". | +| code | [string](#string) | required | A manufacturer code, e.g. "DKL4P". | | details | [string](#string) | optional | Manufacturer details (minimum orders et.c.). | | category | [Manufacturer.Category](#com-example-Manufacturer-Category) | optional | Manufacturer category. Default: CATEGORY_EXTERNAL | @@ -213,8 +213,8 @@ Represents a vehicle model. | Field | Type | Label | Description | | ----- | ---- | ----- | ----------- | | id | [string](#string) | required | The unique model ID. | -| model_code | [string](#string) | required | The car model code, e.g. "PZ003". | -| model_name | [string](#string) | required | The car model name, e.g. "Z3". | +| model_code | [string](#string) | required | The car model code, e.g. "PZ003". | +| model_name | [string](#string) | required | model_name represents the car model name.

This field typically contains the manufacturer name, e.g. "Porsche 911". | | daily_hire_rate_dollars | [sint32](#sint32) | required | Dollars per day. | | daily_hire_rate_cents | [sint32](#sint32) | required | Cents per day. | @@ -257,8 +257,8 @@ Represents a vehicle category. E.g. "Sedan" or "Truck". | Field | Type | Label | Description | | ----- | ---- | ----- | ----------- | -| code | [string](#string) | required | Category code. E.g. "S". | -| description | [string](#string) | required | Category name. E.g. "Sedan". | +| code | [string](#string) | required | Category code. E.g. "S". | +| description | [string](#string) | required | Category name. E.g. "Sedan". | diff --git a/examples/doc/example.txt b/examples/doc/example.txt index db07a128..170d3bc8 100644 --- a/examples/doc/example.txt +++ b/examples/doc/example.txt @@ -158,7 +158,9 @@ Represents a vehicle model. |model_code | <> |required |The car model code, e.g. "PZ003". -|model_name | <> |required |The car model name, e.g. "Z3". +|model_name | <> |required |model_name represents the car model name. + +This field typically contains the manufacturer name, e.g. "Porsche 911". |daily_hire_rate_dollars | <> |required |Dollars per day. diff --git a/examples/proto/Vehicle.proto b/examples/proto/Vehicle.proto index db6daac0..cac869b9 100644 --- a/examples/proto/Vehicle.proto +++ b/examples/proto/Vehicle.proto @@ -38,7 +38,11 @@ extend Manufacturer { message Model { required string id = 1; /** The unique model ID. */ required string model_code = 2; /** The car model code, e.g. "PZ003". */ - required string model_name = 3; /** The car model name, e.g. "Z3". */ + + // model_name represents the car model name. + // + // This field typically contains the manufacturer name, e.g. "Porsche 911". + required string model_name = 3; required sint32 daily_hire_rate_dollars = 4; /// Dollars per day. required sint32 daily_hire_rate_cents = 5; /// Cents per day. diff --git a/filters.go b/filters.go index 6138b786..729b72d5 100644 --- a/filters.go +++ b/filters.go @@ -28,6 +28,18 @@ func ParaFilter(content string) string { // NoBrFilter removes single CR and LF from content. func NoBrFilter(content string) string { + paragraphs := contentToParagraphs(content) + return strings.Join(paragraphs, "\n\n") +} + +// BrFilterMD removes single CR and LF from content and replaces it with unescaped +// HTML line breaks for markdown. +func BrFilterMD(content string) template.HTML { + paragraphs := contentToParagraphs(content) + return template.HTML(strings.Join(paragraphs, "

")) +} + +func contentToParagraphs(content string) []string { normalized := strings.Replace(content, "\r\n", "\n", -1) paragraphs := multiNewlinePattern.Split(normalized, -1) for i, p := range paragraphs { @@ -35,7 +47,7 @@ func NoBrFilter(content string) string { withoutLF := strings.Replace(withoutCR, "\n", " ", -1) paragraphs[i] = spacePattern.ReplaceAllString(withoutLF, " ") } - return strings.Join(paragraphs, "\n\n") + return paragraphs } // AnchorFilter replaces all special characters with URL friendly dashes diff --git a/filters_test.go b/filters_test.go index 353583c4..907c6345 100644 --- a/filters_test.go +++ b/filters_test.go @@ -50,6 +50,20 @@ func TestNoBrFilter(t *testing.T) { } } +func TestBrMdFilter(t *testing.T) { + tests := map[string]string{ + "My content": "My content", + "My content \r\nHere.": "My content Here.", + "My\n content\r right\r\n here.": "My content right here.", + "My\ncontent\rright\r\nhere.": "My content right here.", + "My content.\n\nMore content.": "My content.

More content.", + } + + for input, output := range tests { + require.Equal(t, html.HTML(output), BrFilterMD(input)) + } +} + func TestAnchorFilter(t *testing.T) { tests := map[string]string{ "com/example/test.proto": "com_example_test-proto", diff --git a/renderer.go b/renderer.go index eba74c5f..ce9dd02f 100644 --- a/renderer.go +++ b/renderer.go @@ -78,6 +78,7 @@ var funcMap = map[string]interface{}{ "p": PFilter, "para": ParaFilter, "nobr": NoBrFilter, + "brmd": BrFilterMD, "anchor": AnchorFilter, }