6

[Golang] Wrap Pāli Words in Span Element

 2 years ago
source link: http://siongui.github.io/2016/04/28/go-wrap-pali-words-in-span-element/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

[Golang] Wrap Pāli Words in Span Element

April 28, 2016

Wrap Pāli words in span element via regular expression and Golang (Go programming language).

Source code:

Run Code on Go Playground

pali.go | repository | view raw

package wrappali

import "regexp"

var paliWord = regexp.MustCompile(`[AaBbCcDdEeGgHhIiJjKkLlMmNnOoPpRrSsTtUuVvYyĀāĪīŪūṀṁṂṃŊŋṆṇṄṅÑñṬṭḌḍḶḷ]+`)

func WrapPaliWordsInSpan(html string) string {
	return paliWord.ReplaceAllStringFunc(html, func(match string) string {
		return "<span>" + match + "</span>"
	})
}

pali_test.go | repository | view raw

package wrappali

import "testing"

const html = `1.
Manopubbaṅgamā dhammā, manoseṭṭhā manomayā;
Manasā ce paduṭṭhena, bhāsati vā karoti vā;
Tato naṃ dukkhamanveti, cakkaṃva vahato padaṃ.`

func TestWrapPaliWordsInSpan(t *testing.T) {
	t.Log(WrapPaliWordsInSpan(html))
}

Output:

=== RUN   TestWrapPaliWordsInSpan
--- PASS: TestWrapPaliWordsInSpan (0.00s)
      pali_test.go:11: 1.
              <span>Manopubbaṅgamā</span> <span>dhammā</span>, <span>manoseṭṭhā</span> <span>manomayā</span>;
              <span>Manasā</span> <span>ce</span> <span>paduṭṭhena</span>, <span>bhāsati</span> <span>vā</span> <span>karoti</span> <span>vā</span>;
              <span>Tato</span> <span>naṃ</span> <span>dukkhamanveti</span>, <span>cakkaṃva</span> <span>vahato</span> <span>padaṃ</span>.
PASS

Tested on: Ubuntu Linux 16.04, Go 1.6.2.

References:

[1][JavaScript] Wrap Pāli Words in Span Element

[2]regexp - The Go Programming Language

[3][Golang] Find Pāli Word in Velthuis Scheme


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK