Repository navigation
Expand file tree
/
Copy pathhtml_test.go
More file actions
374 lines (307 loc) · 11.3 KB
/
Copy pathhtml_test.go
File metadata and controls
374 lines (307 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package html
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestHTML(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<!DOCTYPE html>
<html itemscope itemtype="https://schema.org/Article">
<head>
<title>DSN</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<meta name="author" value="wmentor">
<meta name="keywords" content="wmentor,lemma,nlp,golang">
<meta name="description" content=" We often work with configuration strings like pq database connection…" />
<!-- Schema.org markup for Google+ -->
<meta itemprop="name" content="DSN">
<meta itemprop="description" content=" We often work with configuration strings like pq database connection…">
<meta itemprop="image" content="https://wmentor.ru/pics/go/5e67edc8.jpg">
<!-- Twitter Card data -->
<meta name="twitter:card" content="summary">
<meta name="twitter:site" content="@wmentorru">
<meta name="twitter:title" content="DSN">
<meta name="twitter:description" content=" We often work with configuration strings like pq database connection…">
<meta name="twitter:creator" content="@wmentorru">
<meta name="twitter:image" content="https://wmentor.ru/pics/go/5e67edc8.jpg">
<!-- Open Graph -->
<meta property="og:title" content="DSN" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://wmentor.ru/dsn" />
<meta property="og:image" content="https://wmentor.ru/pics/go/5e67edc8.jpg" />
<meta property="og:description" content=" We often work with configuration strings like pq database connection…" />
<meta property="og:site_name" content="WMentor.ru" />
<link rel="shortcut icon" href="/favicon.ico?v=1583781460" ; type="image/x-icon"/>
<link rel="icon" href="/favicon.ico?v=1583781460" ; type="image/x-icon"/>
<link rel="stylesheet" href="/css/styles.css?v=1583781460">
<link rel="stylesheet" href="/css/styles/a11y-light.css?v=1583781460">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="/js/highlight.pack.js?v=1583781460"></script>
<script src="/js/rpc.js?v=1583781460"></script>
<script src="/js/share.js?v=1583781460"></script>
<body>
<div class="menu">
<div class="area">
<table width="100%" cellspacing="0" cellpadding="0" class="mobile_menu">
<tr>
<td style="WIDTH: 40px;"><img src="/images/menu.png?v=1583781460" style="" onclick="$('.menu_widget').css('display','block'); return false
;"/></td>
<td style="COLOR:#FFF;TEXT-ALIGN:CENTER;" onclick="location.href='/';">WMENTOR.RU</td>
<td style="WIDTH: 40px;"><img src="/images/search.png?v=1583781460" style="" onclick="$('.search_widget').css('display','block'); return f
alse;"/></td>
</tr>
</table>
<img src="/images/logo.png?v=1583781460" height="48px" class="logo"/>
<div class="search"><table width="100%"><tr><form action="/" method="GET"><td><input type="text" id="search" name="search" placeholder="Search
..."></td><td><img src="/images/search.png?v=1583781460" height="48px"/></td></form></tr></table></div>
<table class="desktop_menu">
<tr style="VERTICAL-ALIGN:MIDDLE;">
<td><a href="/" class="name">WMentor.ru</a></td>
</table>
</div>
</div>
<div class="menu_widget">
<div class="close" onclick="$('.menu_widget').css('display','none'); return false;">X</div>
<a href="/">Main page</a><br/>
<a href="/golang">Golang</a><br/>
<a href="/hello">About</a><br/>
</div>
<div class="search_widget">
<div class="close" onclick="$('.search_widget').css('display','none'); return false;">X</div>
<form method="GET" action="/" id="mobile_search_form">
<table width="100%">
<tr>
<td style="TEXT-ALIGN: CENTER;COLOR: #FFF;FONT-SIZE: 24px;">SEARCH</td>
</tr>
<tr>
<td style="TEXT-ALIGN:CENTER;"><input type="text" name="search" id="m_search" style="WIDTH:90%;"></td>
</tr>
<tr>
<td style="TEXT-ALIGN: CENTER;"><img src="/images/search.png" width="48px;" height="48px;" onclick="$('#mobile_search_form').submit(); return
false;" /></td>
</tr>
</table>
</form>
</div>
<div class="container">
<div class="r-column">
<br/>
<div class="r-column-widget">
<div class="r-column-widget-head">top tags</div>
<div class="r-column-widget-body">
<a href="/?tag=4">golang</a>
<a href="/?tag=2">nlp</a>
<a href="/?tag=6">go</a>
<a href="/?tag=16">programming</a>
<a href="/?tag=7">text mining</a>
<a href="/?tag=14">lemma</a>
<a href="/?tag=18">it</a>
<a href="/?tag=10">lemmatization</a>
<a href="/?tag=12">text processing</a>
<a href="/?tag=3">tokenizer</a>
<a href="/?tag=15">wmentor</a>
<a href="/?tag=24">configuration</a>
<a href="/?tag=26">data source name</a>
<a href="/?tag=5">development</a>
</div>
</div>
<br/>
<div class="r-column-widget">
<div class="r-column-widget-head">archive</div>
<div class="r-column-widget-body">
<a href="/?period=2020">2020</a>
<a href="/?period=2019">2019</a>
</div>
</div>
<br/>
<div class="r-column-widget">
<div class="r-column-widget-head">archive</div>
<div class="r-column-widget-body">
<a href="/?period=2020">2020</a>
<a href="/?period=2019">2019</a>
</div>
</div>
<br/>
<div class="r-column-widget">
<div class="r-column-widget-head">follow me</div>
<div class="r-column-widget-body">
<a href="https://twitter.com/wmentorru" target="_blank"><img class="social-icon-small social-tw" src="/images/social/tw.png?v=1583781460"></a>
<a href="https://t.me/wmentor" target="_blank"><img class="social-icon-small social-tg" src="/images/social/tg.png?v=1583781460"></a>
<a href="/rss.xml" title="RSS" target="_blank"><img class="social-icon-small social-rss" src="/images/social/rss.png?v=1583781460"></a>
<a href="https://github.com/wmentor" title="GitHub" target="_blank"><img class="social-icon-small social-github" src="/images/social/github.pn
g?v=1583781460"></a>
</div>
</div>
<br/>
</div><div class="l-column">
<div>
<h1>DSN</h1>
<p>We often work with configuration strings like pq database connection:</p>
<pre><code class="plaintext">user=mylogin password=mypass database=mydb host=127.0.0.1 port=5432
</code></pre>
<figure>
<img src="/pics/go/5e67edc8.jpg"/>
</figure>
<p>The simplest way to parse them in Golang is regular expressions:</p>
<pre><code class="go">package main
import (
"fmt"
"regexp"
)
var rex = regexp.MustCompile("(\\w+)=(\\w+)")
func main() {
conn := "user=mylogin password=mypass database=mydb host=127.0.0.1 port=5432 sslmode=true"
data := rex.FindAllStringSubmatch(conn, -1)
res := make(map[string]string)
for _, kv := range data {
k := kv[1]
v := kv[2]
res[k] = v
}
fmt.Println(res)
}
</code></pre>
<p>But if the value can contain a space character, then you need to add escaping support in a regular expression. And so on, each add[169/579]
ction makes processing harder. But there is an easier way (wmentor/dsn).</p>
<p>Install package:</p>
<pre><code class="plaintext">go get github.com/wmentor/dsn
</code></pre>
<p>Usage:</p>
<pre><code class="go">package main
import (
"fmt"
"github.com/wmentor/dsn"
)
func main() { [151/579]
str := "user=mylogin passwd=mypass database=mydb port=5432 sslmode=true"
ds, err := dsn.New(str)
if err != nil {
panic("invalid string")
}
// print user=mylogin
fmt.Printf( "user=%s\n", ds.GetString("user","unknown") )
// print passwd=mypass
fmt.Printf( "passwd=%s\n", ds.GetString("passwd","nopass") )
// host is not exists, print host=127.0.0.1
fmt.Printf( "host=%s\n", ds.GetString("host","127.0.0.1") )
// get int value and print port=5432
fmt.Printf( "port=%d\n", ds.GetInt("port", 4321) )
// print sslmode=true
fmt.Printf( "sslmode=%t\n", ds.GetBool("sslmode", false) )
// print keepalive=false
fmt.Printf( "keepalive=%t\n", ds.GetBool("keepalive", false) )
}
</code></pre>
<p><b>dns.New</b> returns object <b>dsn.DSN</b> or an error. All get methods (<i>GetString</i>, <i>GetBool</i>, <i>GetInt</i>, <i>GetInt64</i$
, <i>GetFloat</i>) take 2 arguments - key name and default value. The default value is used when the key is missing or contains a invalid val$
e.</p>
<p>Moreover, dsn support escape some characters in key name and value (\s,\t,\r,\n,\=,\\,\",\').</p>
<script language="javascript">
<!--
var uid = "fewrretwertwertwert";
SharingOpts = {"url":"https://wmentor.ru/dsn?v=1","title":"DSN","text":" We often work with configuration strings like pq database connection…
","image":"https://wmentor.ru/pics/go/5e67edc8.jpg"};
-->
</script>
</body>
</html>
`))
}))
defer ts.Close()
parser := New()
err := parser.Get(ts.URL, nil)
if err != nil {
t.Fatal("HTML.Get failed")
}
txt := string(parser.Text())
waitText := `WMENTOR.RU
WMentor.ru
X
Main page
Golang
About
X
top tags
golang nlp go programming text mining lemma it lemmatization text processing tokenizer wmentor configuration data source name development
archive
2020 2019
archive
2020 2019
follow me
DSN
We often work with configuration strings like pq database connection:
user=mylogin password=mypass database=mydb host=127.0.0.1 port=5432 The simplest way to parse them in Golang is regular expressions:
package main
import (
"fmt"
"regexp"
)
var rex = regexp.MustCompile("(\\w+)=(\\w+)")
func main() {
conn := "user=mylogin password=mypass database=mydb host=127.0.0.1 port=5432 sslmode=true"
data := rex.FindAllStringSubmatch(conn, -1)
res := make(map[string]string)
for _, kv := range data {
k := kv[1]
v := kv[2]
res[k] = v
}
fmt.Println(res)
} But if the value can contain a space character, then you need to add escaping support in a regular expression. And so on, each add[169/579]
ction makes processing harder. But there is an easier way (wmentor/dsn).
Install package:
go get github.com/wmentor/dsn Usage:
package main
import (
"fmt"
"github.com/wmentor/dsn"
)
func main() { [151/579]
str := "user=mylogin passwd=mypass database=mydb port=5432 sslmode=true"
ds, err := dsn.New(str)
if err != nil {
panic("invalid string")
}
// print user=mylogin
fmt.Printf( "user=%s\n", ds.GetString("user","unknown") )
// print passwd=mypass
fmt.Printf( "passwd=%s\n", ds.GetString("passwd","nopass") )
// host is not exists, print host=127.0.0.1
fmt.Printf( "host=%s\n", ds.GetString("host","127.0.0.1") )
// get int value and print port=5432
fmt.Printf( "port=%d\n", ds.GetInt("port", 4321) )
// print sslmode=true
fmt.Printf( "sslmode=%t\n", ds.GetBool("sslmode", false) )
// print keepalive=false
fmt.Printf( "keepalive=%t\n", ds.GetBool("keepalive", false) )
} dns.New returns object dsn.DSN or an error. All get methods ( GetString , GetBool , GetInt , GetInt64 GetFloat ) take 2 arguments - key name and default value. The default value is used when the key is missing or contains a invalid val$
e.
Moreover, dsn support escape some characters in key name and value (\s,\t,\r,\n,\=,\\,\",\').
`
if waitText != txt {
t.Fatal("Invalid result")
}
totalUrls := 0
parser.EachLink(func(str string) {
totalUrls++
})
if totalUrls != 23 {
t.Fatal("EachLink failed")
}
totalImg := 0
parser.EachImage(func(str string) {
totalImg++
})
if totalImg != 9 {
t.Fatal("EachImg failed")
}
totalIframes := 0
parser.EachIframe(func(str string) {
totalIframes++
})
if totalIframes != 0 {
t.Fatal("EachIframe failed")
}
}