FAQ
Reviewers: golang-dev_googlegroups.com,

Message:
Hello golang-dev@googlegroups.com (cc: golang-dev@googlegroups.com),

I'd like you to review this change to
https://code.google.com/p/go.talks


Description:
go.talks/pkg/present: Adding inline links with style.

Read doc.go for more details.

Please review this at https://codereview.appspot.com/6847128/

Affected files:
M pkg/present/doc.go
M pkg/present/link.go
M pkg/present/style.go
M pkg/present/style_test.go


Index: pkg/present/doc.go
===================================================================
--- a/pkg/present/doc.go
+++ b/pkg/present/doc.go
@@ -73,6 +73,14 @@
_this_is_all_italic_
_Why_use_scoped__ptr_? Use plain ***ptr* instead.

+Inline links:
+
+Links can be inlined in the text using the syntax [[url][text]], or
+[[url]] if the text to be used in the link is the same url. The font
+modifiers introduced above can be used in the `text` part.
+
+Note that characters `[` and `]` are not allowed in neither url nor text.
+
Functions:

A number of template functions are available through invocations
Index: pkg/present/link.go
===================================================================
--- a/pkg/present/link.go
+++ b/pkg/present/link.go
@@ -8,6 +8,7 @@
"fmt"
"html/template"
"net/url"
+ "regexp"
"strings"
)

@@ -48,3 +49,25 @@
}
return template.HTML(fmt.Sprintf(`<a href=%q>%s</a>`, url.String(),
label)), nil
}
+
+var inlineExp = regexp.MustCompile(`^\[\[([^\[\]]+)\](\[([^\[\]]*)\])?\]`)
+
+func isInlineLink(text string) bool {
+ return inlineExp.MatchString(text)
+}
+
+func findInlineLink(text string, start int) (int, int, bool) {
+ if f := inlineExp.FindString(text[start:]); f != "" {
+ return start, start + len(f), true
+ }
+ return 0, 0, false
+}
+
+func parseInlineLink(text string) string {
+ s := inlineExp.FindStringSubmatch(text)
+ url, text := s[1], font(s[3])
+ if text == "" {
+ text = url
+ }
+ return fmt.Sprintf(`<a href=%q>%s</a>`, url, text)
+}
Index: pkg/present/style.go
===================================================================
--- a/pkg/present/style.go
+++ b/pkg/present/style.go
@@ -36,7 +36,7 @@

// font returns s with font indicators turned into HTML font tags.
func font(s string) string {
- if strings.IndexAny(s, "`_*") == -1 {
+ if strings.IndexAny(s, "[`_*") == -1 {
return s
}
words := split(s)
@@ -46,6 +46,10 @@
if len(word) < 2 {
continue Word
}
+ if isInlineLink(word) {
+ words[w] = parseInlineLink(word)
+ continue Word
+ }
const punctuation = `.,;:()!?—–'"`
const marker = "_*`"
// Initial punctuation is OK but must be peeled off.
@@ -125,6 +129,10 @@
words = append(words, s[mark:i])
mark = i
}
+ if start, end, found := findInlineLink(s, i); found {
+ words = append(words, s[start:end])
+ mark = end
+ }
prevWasSpace = isSpace
}
if mark < len(s) {
Index: pkg/present/style_test.go
===================================================================
--- a/pkg/present/style_test.go
+++ b/pkg/present/style_test.go
@@ -19,6 +19,14 @@
{"abc", []string{"abc"}},
{"abc def", []string{"abc", " ", "def"}},
{"abc def ", []string{"abc", " ", "def", " "}},
+ {"hey [[http://go/go][Gophers]] around",
+ []string{"hey", " ", "[[http://go/go][Gophers]]", " ", "around"}},
+ {"A [[http://go/go][two words]] link",
+ []string{"A", " ", "[[http://go/go][two words]]", " ", "link"}},
+ {"Visit [[http://go/go]] now",
+ []string{"Visit", " ", "[[http://go/go]]", " ", "now"}},
+ {"not [[http://go/go][a [[link]] ]] around",
+
[]string{"not", " ", "[[http://go/go][a", " ", "[[link]]", " ", "]]", " ", "around"}},
}
for _, test := range tests {
out := split(test.in)
@@ -49,6 +57,9 @@
{"(_a)", "(_a)"},
{"(_a)", "(_a)"},
{"_Why_use_scoped__ptr_? Use plain ***ptr* instead.", "<i>Why use
scoped_ptr</i>? Use plain <b>*ptr</b> instead."},
+ {"_hey_ [[http://go/go][*Gophers*]] *around*",
+ "<i>hey</i> <a href=\"http://go/go\"><b>Gophers</b></a> <b>around</b>"},
+ {"Visit [[http://go/go]] now", "Visit <a
href=\"http://go/go\">http://go/go</a> now"},
}
for _, test := range tests {
out := font(test.in)

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-dev @
categoriesgo
postedDec 1, '12 at 12:32a
activeDec 1, '12 at 8:54p
posts3
users2
websitegolang.org

2 users in discussion

Campoy: 2 posts Adg: 1 post

People

Translate

site design / logo © 2022 Grokbase