present: allow markup inside punctuation connected to other text

This allows markup bracketed by punctuation even when the
punctuation has text on the other side, like in:

- Markup—_especially_italic_text_—can easily be overused.
- We want to increase `go`vet`'s usage.

Change-Id: I0c6ca790f23f705d8c8ba8a225c0280b916ebb6c
Reviewed-on: https://go-review.googlesource.com/33662
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
This commit is contained in:
Russ Cox 2016-11-29 13:39:51 -05:00
Родитель e5f9a3deee
Коммит e04df2157a
3 изменённых файлов: 29 добавлений и 12 удалений

Просмотреть файл

@ -77,14 +77,22 @@ Fonts:
Within the input for plain text or lists, text bracketed by font
markers will be presented in italic, bold, or program font.
Marker characters are _ (italic), * (bold) and ` (program font).
Unmatched markers appear as plain text.
Within marked text, a single marker character becomes a space
and a doubled single marker quotes the marker character.
An opening marker must be preceded by a space or punctuation
character or else be at start of a line; similarly, a closing
marker must be followed by a space or punctuation character or
else be at the end of a line. Unmatched markers appear as plain text.
There must be no spaces between markers. Within marked text,
a single marker character becomes a space and a doubled single
marker quotes the marker character.
at the beginning of a line or
else be preceded by a space or punctuation; similarly a closing
marker must be at the end of the lineo
_italic_
*bold*
`program`
_this_is_all_italic_
Markup_especially_italic_text_can easily be overused.
_Why_use_scoped__ptr_? Use plain ***ptr* instead.
Inline links:

Просмотреть файл

@ -52,16 +52,16 @@ Word:
words[w] = link
continue Word
}
const punctuation = `.,;:()!?—–'"`
const marker = "_*`"
// Initial punctuation is OK but must be peeled off.
first := strings.IndexAny(word, marker)
if first == -1 {
continue Word
}
// Is the marker prefixed only by punctuation?
for _, r := range word[:first] {
if !strings.ContainsRune(punctuation, r) {
// Opening marker must be at the beginning of the token or else preceded by punctuation.
if first != 0 {
r, _ := utf8.DecodeLastRuneInString(word[:first])
if !unicode.IsPunct(r) {
continue Word
}
}
@ -81,17 +81,18 @@ Word:
open += "<code>"
close = "</code>"
}
// Terminal punctuation is OK but must be peeled off.
// Closing marker must be at the end of the token or else followed by punctuation.
last := strings.LastIndex(word, word[:1])
if last == 0 {
continue Word
}
head, tail := word[:last+1], word[last+1:]
for _, r := range tail {
if !strings.ContainsRune(punctuation, r) {
if last+1 != len(word) {
r, _ := utf8.DecodeRuneInString(word[last+1:])
if !unicode.IsPunct(r) {
continue Word
}
}
head, tail := word[:last+1], word[last+1:]
b.Reset()
b.WriteString(open)
var wid int

Просмотреть файл

@ -72,6 +72,14 @@ func TestFont(t *testing.T) {
`Visit <a href="http://golang.org" target="_blank">golang.org</a> now`},
{"my talk ([[http://talks.golang.org/][slides here]])",
`my talk (<a href="http://talks.golang.org/" target="_blank">slides here</a>)`},
{"Markup—_especially_italic_text_—can easily be overused.",
`Markup—<i>especially italic text</i>—can easily be overused.`},
{"`go`get`'s codebase", // ascii U+0027 ' before s
`<code>go get</code>'s codebase`},
{"`go`get`s codebase", // unicode right single quote U+2019 before s
`<code>go get</code>s codebase`},
{"a_variable_name",
`a_variable_name`},
}
for _, test := range tests {
out := font(test.in)