Best K6 code snippet using html.InnerHTML
blackjack.go
Source:blackjack.go
1package main2import (3 "log"4 "net"5 "net/http"6 "strconv"7 //"fmt"8 "./blackjack"9 "./cards"10 "github.com/zserge/webview"11)12var indexHTML = `13<!doctype html>14<html>15 <head>16 <meta http-equiv="X-UA-Compatible" content="IE=edge">17 </head>18 <body>19 <button id = "start" onclick="external.invoke('start')">Start</button>20 <button id = "hit" onclick="external.invoke('hit')">Hit</button>21 <button id = "stand" onclick="external.invoke('stand')">Stand</button>22 <button id = "double" onclick="external.invoke('double')">Double</button>23 <button id = "split" onclick="external.invoke('split')">Split</button>24 <button id = "settle" onclick="external.invoke('settle')">Settle</button>25 <label id = "turn" >Blank</label>26 <label id = "walletTitle"> Wallet:</label>27 <label id = "betTitle"> Bet:</label>28 <div id="dealer">29 <h2> dealer</h2>30 <p>blank</p> 31 </div>32 <div id = "player1">33 <h2>player1</h2>34 <p>blank</p>35 <p class = "wallet"></p>36 <p class = "bet"></p>37 <p class = "splithand"></p>38 </div>39 <div id = "player2">40 <h2>player2</h2>41 <p>blank</p>42 <p class = "wallet"></p>43 <p class = "bet"></p>44 <p class = "splithand"></p>45 </div>46 <div id = "player3">47 <h2>player3</h2>48 <p>blank</p>49 <p class = "wallet"></p>50 <p class = "bet"></p>51 <p class = "splithand"></p>52 </div>53 54 </body>55 <style> 56 #dealer57 {58 position:absolute;59 left:400px;60 top:50px61 }62 #player163 {64 position:absolute;65 left:100px;66 top:300px67 }68 #player269 {70 position:absolute;71 left:500px;72 top:300px73 }74 #player375 {76 position:absolute;77 left:900px;78 top:300px79 }80 #walletTitle81 {82 position:absolute;83 left:10px;84 top:400px85 }86 #betTitle87 {88 position:absolute;89 left:10px;90 top:440px91 }92 #hit93 {94 visibility:hidden95 }96 #stand97 {98 visibility:hidden99 }100 #double101 {102 visibility:hidden103 }104 #split105 {106 visibility:hidden107 }108 #settle109 {110 visibility:hidden111 } 112 </style> 113 <script>114 115 </script>116</html>117`118var deck = cards.NewDeck(6)119var dealersHand = new(blackjack.Hand)120var player1Hand = new(blackjack.Hand)121var player2Hand = new(blackjack.Hand)122var player3Hand = new(blackjack.Hand)123var player1Split = new(blackjack.Hand)124var player2Split = new(blackjack.Hand)125var player3Split = new(blackjack.Hand)126var turn = "blank"127func startServer() string {128 ln, err := net.Listen("tcp", "127.0.0.1:0")129 if err != nil {130 log.Fatal(err)131 }132 go func() {133 defer ln.Close()134 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {135 w.Write([]byte(indexHTML))136 })137 log.Fatal(http.Serve(ln, nil))138 }()139 return "http://" + ln.Addr().String()140}141func handleRPC(w webview.WebView, data string) {142 switch {143 case data == "stand":144 stand(w)145 case data == "start":146 start(w)147 case data == "hit":148 hit(w)149 case data == "settle":150 settle(w)151 case data == "double":152 double(w)153 case data == "split":154 split(w)155 }156}157func main() {158 url := startServer()159 w := webview.New(webview.Settings{160 Width: 1200,161 Height: 800,162 Title: "blackjack demo",163 Resizable: true,164 URL: url,165 ExternalInvokeCallback: handleRPC,166 })167 defer w.Exit()168 w.Run()169}170func double(w webview.WebView) {171 if turn == "player1's turn" {172 if player1Hand.CanDouble() && player1Hand.CanBet() {173 player1Hand.Double()174 if player1Hand.Score() > 21 {175 w.Dialog(webview.DialogTypeAlert, 0, "Player1", "Player1 bust!\n")176 }177 if player2Hand.Bet != 0 {178 turn = "player2's turn"179 } else if player3Hand.Bet != 0 {180 turn = "player3's turn"181 } else {182 turn = "dealer's turn"183 w.Eval(`document.getElementById("dealer").getElementsByTagName("p")[0].innerHTML += ",` + dealersHand.Cards[1].ToStr() + `";`)184 w.Eval(`document.getElementById("settle").style.visibility = "visible";`)185 w.Eval(`document.getElementById("hit").style.visibility = "hidden";`)186 w.Eval(`document.getElementById("stand").style.visibility = "hidden";`)187 w.Eval(`document.getElementById("double").style.visibility = "hidden";`)188 }189 w.Dispatch(func() {190 w.Eval(`document.getElementById("turn").innerHTML = "` + turn + `";`)191 w.Eval(`document.getElementById("player1").getElementsByTagName("p")[0].innerHTML = "` + player1Hand.ToStr() + `";`)192 w.Eval(`document.getElementById("player1").getElementsByClassName("wallet")[0].innerHTML = "` + strconv.Itoa(player1Hand.Wallet) + `";`)193 w.Eval(`document.getElementById("player1").getElementsByClassName("bet")[0].innerHTML = "` + strconv.Itoa(player1Hand.Bet) + `";`)194 })195 } else {196 w.Dialog(webview.DialogTypeAlert, 0, "Player1", "Player1 can not double!\n")197 }198 } else if turn == "player2's turn" {199 if player2Hand.CanDouble() && player2Hand.CanBet() {200 player2Hand.Double()201 if player2Hand.Score() > 21 {202 w.Dialog(webview.DialogTypeAlert, 0, "Player2", "Player2 bust!\n")203 }204 if player3Hand.Bet != 0 {205 turn = "player3's turn"206 } else {207 turn = "dealer's turn"208 w.Eval(`document.getElementById("dealer").getElementsByTagName("p")[0].innerHTML += ",` + dealersHand.Cards[1].ToStr() + `";`)209 w.Eval(`document.getElementById("settle").style.visibility = "visible";`)210 w.Eval(`document.getElementById("hit").style.visibility = "hidden";`)211 w.Eval(`document.getElementById("stand").style.visibility = "hidden";`)212 w.Eval(`document.getElementById("double").style.visibility = "hidden";`)213 }214 w.Dispatch(func() {215 w.Eval(`document.getElementById("turn").innerHTML = "` + turn + `";`)216 w.Eval(`document.getElementById("player2").getElementsByTagName("p")[0].innerHTML = "` + player2Hand.ToStr() + `";`)217 w.Eval(`document.getElementById("player2").getElementsByClassName("wallet")[0].innerHTML = "` + strconv.Itoa(player2Hand.Wallet) + `";`)218 w.Eval(`document.getElementById("player2").getElementsByClassName("bet")[0].innerHTML = "` + strconv.Itoa(player2Hand.Bet) + `";`)219 })220 } else {221 w.Dialog(webview.DialogTypeAlert, 0, "Player2", "Player2 can not double!\n")222 }223 } else if turn == "player3's turn" {224 if player3Hand.CanDouble() && player3Hand.CanBet() {225 player3Hand.Double()226 if player3Hand.Score() > 21 {227 w.Dialog(webview.DialogTypeAlert, 0, "Player3", "Player3 bust!\n")228 }229 turn = "dealer's turn"230 w.Eval(`document.getElementById("dealer").getElementsByTagName("p")[0].innerHTML += ",` + dealersHand.Cards[1].ToStr() + `";`)231 w.Eval(`document.getElementById("settle").style.visibility = "visible";`)232 w.Eval(`document.getElementById("hit").style.visibility = "hidden";`)233 w.Eval(`document.getElementById("stand").style.visibility = "hidden";`)234 w.Eval(`document.getElementById("double").style.visibility = "hidden";`)235 w.Dispatch(func() {236 w.Eval(`document.getElementById("turn").innerHTML = "` + turn + `";`)237 w.Eval(`document.getElementById("player3").getElementsByTagName("p")[0].innerHTML = "` + player3Hand.ToStr() + `";`)238 w.Eval(`document.getElementById("player3").getElementsByClassName("wallet")[0].innerHTML = "` + strconv.Itoa(player3Hand.Wallet) + `";`)239 w.Eval(`document.getElementById("player3").getElementsByClassName("bet")[0].innerHTML = "` + strconv.Itoa(player3Hand.Bet) + `";`)240 })241 } else {242 w.Dialog(webview.DialogTypeAlert, 0, "Player3", "Player3 can not double!\n")243 }244 } else if turn == "dealer's turn" {245 w.Dialog(webview.DialogTypeAlert, 0, "Dealer", "dealer can not double!\n")246 }247}248func settle(w webview.WebView) {249 if turn == "dealer's turn" {250 for dealersHand.Score() < 17 {251 card := dealersHand.Hit()252 w.Eval(`document.getElementById("dealer").getElementsByTagName("p")[0].innerHTML += ",` + card.ToStr() + `";`)253 }254 w.Eval(`document.getElementById("dealer").getElementsByTagName("p")[0].innerHTML = "` + dealersHand.ToStr() + `";`)255 if player1Hand.HasSplit {256 if player1Hand.Score() == 21 || player1Split.Score() == 21 {257 if dealersHand.Score() != 21 {258 player1Hand.Wallet += player1Hand.Bet * 2259 }260 } else if player1Hand.IsBust == true && player1Split.IsBust == true {261 if dealersHand.Score() > 21 {262 player1Hand.Wallet += player1Hand.Bet263 }264 } else {265 if dealersHand.Score() > 21 {266 player1Hand.Wallet += player1Hand.Bet * 2267 } else if dealersHand.Score() > player1Hand.Score() && dealersHand.Score() > player1Split.Score() {268 } else if dealersHand.Score() < player1Hand.Score() || dealersHand.Score() < player1Split.Score() {269 player1Hand.Wallet += player1Hand.Bet * 2270 } else {271 player1Hand.Wallet += player1Hand.Bet272 }273 }274 } else {275 if player1Hand.Score() == 21 {276 if dealersHand.Score() != 21 {277 player1Hand.Wallet += player1Hand.Bet * 2278 }279 } else if player1Hand.IsBust == true {280 if dealersHand.Score() > 21 {281 player1Hand.Wallet += player1Hand.Bet282 }283 } else {284 if dealersHand.Score() > 21 {285 player1Hand.Wallet += player1Hand.Bet * 2286 } else if dealersHand.Score() > player1Hand.Score() {287 } else if dealersHand.Score() < player1Hand.Score() {288 player1Hand.Wallet += player1Hand.Bet * 2289 } else {290 player1Hand.Wallet += player1Hand.Bet291 }292 }293 }294 if player2Hand.HasSplit {295 if player2Hand.Score() == 21 || player2Split.Score() == 21 {296 if dealersHand.Score() != 21 {297 player2Hand.Wallet += player2Hand.Bet * 2298 }299 } else if player2Hand.IsBust == true && player2Split.IsBust == true {300 if dealersHand.Score() > 21 {301 player2Hand.Wallet += player2Hand.Bet302 }303 } else {304 if dealersHand.Score() > 21 {305 player2Hand.Wallet += player2Hand.Bet * 2306 } else if dealersHand.Score() > player2Hand.Score() && dealersHand.Score() > player2Split.Score() {307 } else if dealersHand.Score() < player2Hand.Score() || dealersHand.Score() < player2Split.Score() {308 player2Hand.Wallet += player2Hand.Bet * 2309 } else {310 player2Hand.Wallet += player2Hand.Bet311 }312 }313 } else {314 if player2Hand.Score() == 21 {315 if dealersHand.Score() != 21 {316 player2Hand.Wallet += player2Hand.Bet * 2317 }318 } else if player2Hand.IsBust == true {319 if dealersHand.Score() > 21 {320 player2Hand.Wallet += player2Hand.Bet321 }322 } else {323 if dealersHand.Score() > 21 {324 player2Hand.Wallet += player2Hand.Bet * 2325 } else if dealersHand.Score() > player2Hand.Score() {326 } else if dealersHand.Score() < player2Hand.Score() {327 player2Hand.Wallet += player2Hand.Bet * 2328 } else {329 player2Hand.Wallet += player2Hand.Bet330 }331 }332 }333 if player3Hand.HasSplit {334 if player3Hand.Score() == 21 || player3Split.Score() == 21 {335 if dealersHand.Score() != 21 {336 player3Hand.Wallet += player3Hand.Bet * 2337 }338 } else if player3Hand.IsBust == true && player3Split.IsBust == true {339 if dealersHand.Score() > 21 {340 player3Hand.Wallet += player3Hand.Bet341 }342 } else {343 if dealersHand.Score() > 21 {344 player3Hand.Wallet += player3Hand.Bet * 2345 } else if dealersHand.Score() > player3Hand.Score() && dealersHand.Score() > player3Split.Score() {346 } else if dealersHand.Score() < player3Hand.Score() || dealersHand.Score() < player3Split.Score() {347 player3Hand.Wallet += player3Hand.Bet * 2348 } else {349 player3Hand.Wallet += player3Hand.Bet350 }351 }352 } else {353 if player3Hand.Score() == 21 {354 if dealersHand.Score() != 21 {355 player3Hand.Wallet += player3Hand.Bet * 2356 }357 } else if player3Hand.IsBust == true {358 if dealersHand.Score() > 21 {359 player3Hand.Wallet += player3Hand.Bet360 }361 } else {362 if dealersHand.Score() > 21 {363 player3Hand.Wallet += player3Hand.Bet * 2364 } else if dealersHand.Score() > player3Hand.Score() {365 } else if dealersHand.Score() < player3Hand.Score() {366 player3Hand.Wallet += player3Hand.Bet * 2367 } else {368 player3Hand.Wallet += player3Hand.Bet369 }370 }371 }372 turn = "isSettled"373 w.Dispatch(func() {374 w.Eval(`document.getElementById("turn").innerHTML = "` + turn + `";`)375 w.Eval(`document.getElementById("player1").getElementsByClassName("wallet")[0].innerHTML = "` + strconv.Itoa(player1Hand.Wallet) + `";`)376 w.Eval(`document.getElementById("player2").getElementsByClassName("wallet")[0].innerHTML = "` + strconv.Itoa(player2Hand.Wallet) + `";`)377 w.Eval(`document.getElementById("player3").getElementsByClassName("wallet")[0].innerHTML = "` + strconv.Itoa(player3Hand.Wallet) + `";`)378 })379 w.Eval(`document.getElementById("settle").style.visibility = "hidden";`)380 w.Eval(`document.getElementById("start").style.visibility = "visible";`)381 if player1Hand.HasSplit {382 player1Split.Flush()383 w.Eval(`document.getElementById("player1").getElementsByClassName("splithand")[0].style.visibility = "hidden";`)384 }385 if player2Hand.HasSplit {386 player2Split.Flush()387 w.Eval(`document.getElementById("player2").getElementsByClassName("splithand")[0].style.visibility = "hidden";`)388 }389 if player3Hand.HasSplit {390 player3Split.Flush()391 w.Eval(`document.getElementById("player3").getElementsByClassName("splithand")[0].style.visibility = "hidden";`)392 }393 player1Hand.Flush()394 player2Hand.Flush()395 player3Hand.Flush()396 }397}398func start(w webview.WebView) {399 if turn == "blank" {400 dealersHand = blackjack.NewHand(deck)401 player1Hand = blackjack.NewHand(deck)402 player2Hand = blackjack.NewHand(deck)403 player3Hand = blackjack.NewHand(deck)404 player1Hand.PutBet()405 player2Hand.PutBet()406 player3Hand.PutBet()407 turn = "player1's turn"408 w.Dispatch(func() {409 w.Eval(`document.getElementById("hit").style.visibility = "visible";`)410 w.Eval(`document.getElementById("stand").style.visibility = "visible";`)411 w.Eval(`document.getElementById("double").style.visibility = "visible";`)412 w.Eval(`document.getElementById("split").style.visibility = "visible";`)413 w.Eval(`document.getElementById("dealer").getElementsByTagName("p")[0].innerHTML = "` + dealersHand.Cards[0].ToStr() + `";`)414 w.Eval(`document.getElementById("player1").getElementsByTagName("p")[0].innerHTML = "` + player1Hand.ToStr() + `";`)415 w.Eval(`document.getElementById("player1").getElementsByClassName("wallet")[0].innerHTML = "` + strconv.Itoa(player1Hand.Wallet) + `";`)416 w.Eval(`document.getElementById("player1").getElementsByClassName("bet")[0].innerHTML = "` + strconv.Itoa(player1Hand.Bet) + `";`)417 w.Eval(`document.getElementById("player2").getElementsByTagName("p")[0].innerHTML = "` + player2Hand.ToStr() + `";`)418 w.Eval(`document.getElementById("player2").getElementsByClassName("wallet")[0].innerHTML = "` + strconv.Itoa(player2Hand.Wallet) + `";`)419 w.Eval(`document.getElementById("player2").getElementsByClassName("bet")[0].innerHTML = "` + strconv.Itoa(player2Hand.Bet) + `";`)420 w.Eval(`document.getElementById("player3").getElementsByTagName("p")[0].innerHTML = "` + player3Hand.ToStr() + `";`)421 w.Eval(`document.getElementById("player3").getElementsByClassName("wallet")[0].innerHTML = "` + strconv.Itoa(player3Hand.Wallet) + `";`)422 w.Eval(`document.getElementById("player3").getElementsByClassName("bet")[0].innerHTML = "` + strconv.Itoa(player3Hand.Bet) + `";`)423 })424 if player1Hand.IsBlackjack() {425 //w.Dialog(webview.DialogTypeAlert, 0, "Blackjack", "Blackjack!")426 if dealersHand.ScoreCard(0) >= 10 {427 w.Dispatch(func() {428 w.Eval(`document.getElementById("dealer").innerHTML = ` + dealersHand.ToStr() + `;`)429 })430 }431 if dealersHand.IsBlackjack() {432 //w.Dialog(webview.DialogTypeAlert, 0, "Blackjack", "Blackjack!The game is a push\n")433 } else {434 //player1Hand.Wallet+=player1Hand.Bet435 w.Eval(`document.getElementById("player1").getElementsByClassName("wallet")[0].innerHTML = "` + strconv.Itoa(player1Hand.Wallet) + `";`)436 }437 }438 if player2Hand.IsBlackjack() {439 //w.Dialog(webview.DialogTypeAlert, 0, "Blackjack", "Blackjack!")440 if dealersHand.ScoreCard(0) >= 10 {441 w.Dispatch(func() {442 w.Eval(`document.getElementById("dealer").innerHTML = ` + dealersHand.ToStr() + `;`)443 })444 }445 if dealersHand.IsBlackjack() {446 //w.Dialog(webview.DialogTypeAlert, 0, "Blackjack", "Blackjack!The game is a push\n")447 } else {448 //player2Hand.Wallet+=player2Hand.Bet449 w.Eval(`document.getElementById("player2").getElementsByClassName("wallet")[0].innerHTML = "` + strconv.Itoa(player1Hand.Wallet) + `";`)450 }451 }452 if player3Hand.IsBlackjack() {453 //w.Dialog(webview.DialogTypeAlert, 0, "Blackjack", "Blackjack!")454 if dealersHand.ScoreCard(0) >= 10 {455 w.Dispatch(func() {456 w.Eval(`document.getElementById("dealer").innerHTML = ` + dealersHand.ToStr() + `;`)457 })458 }459 if dealersHand.IsBlackjack() {460 //w.Dialog(webview.DialogTypeAlert, 0, "Blackjack", "Blackjack!The game is a push\n")461 } else {462 //player3Hand.Wallet+=player3Hand.Bet463 w.Eval(`document.getElementById("player3").getElementsByClassName("wallet")[0].innerHTML = "` + strconv.Itoa(player1Hand.Wallet) + `";`)464 }465 }466 w.Dispatch(func() {467 w.Eval(`document.getElementById("turn").innerHTML = "` + turn + `";`)468 w.Eval(`document.getElementById("start").style.visibility = "hidden";`)469 w.Eval(`document.getElementById("settle").style.visibility = "hidden";`)470 })471 } else if turn == "isSettled" {472 dealersHand = blackjack.NewHand(deck)473 if player1Hand.CanBet() {474 player1Hand.Refresh()475 player1Hand.PutBet()476 turn = "player1's turn"477 if player2Hand.CanBet() {478 player2Hand.Refresh()479 player2Hand.PutBet()480 }481 if player3Hand.CanBet() {482 player3Hand.Refresh()483 player3Hand.PutBet()484 }485 } else {486 if player2Hand.CanBet() {487 player2Hand.Refresh()488 player2Hand.PutBet()489 turn = "player2's turn"490 if player3Hand.CanBet() {491 player3Hand.Refresh()492 player3Hand.PutBet()493 }494 } else {495 if player3Hand.CanBet() {496 player3Hand.Refresh()497 player3Hand.PutBet()498 turn = "player3's turn"499 } else {500 w.Dialog(webview.DialogTypeAlert, 0, "Players", "Your wallet is empty! Game Over!\n")501 w.Terminate()502 }503 }504 }505 w.Dispatch(func() {506 w.Eval(`document.getElementById("hit").style.visibility = "visible";`)507 w.Eval(`document.getElementById("stand").style.visibility = "visible";`)508 w.Eval(`document.getElementById("double").style.visibility = "visible";`)509 w.Eval(`document.getElementById("split").style.visibility = "visible";`)510 w.Eval(`document.getElementById("dealer").getElementsByTagName("p")[0].innerHTML = "` + dealersHand.Cards[0].ToStr() + `";`)511 w.Eval(`document.getElementById("player1").getElementsByTagName("p")[0].innerHTML = "` + player1Hand.ToStr() + `";`)512 w.Eval(`document.getElementById("player1").getElementsByClassName("wallet")[0].innerHTML = "` + strconv.Itoa(player1Hand.Wallet) + `";`)513 w.Eval(`document.getElementById("player1").getElementsByClassName("bet")[0].innerHTML = "` + strconv.Itoa(player1Hand.Bet) + `";`)514 w.Eval(`document.getElementById("player2").getElementsByTagName("p")[0].innerHTML = "` + player2Hand.ToStr() + `";`)515 w.Eval(`document.getElementById("player2").getElementsByClassName("wallet")[0].innerHTML = "` + strconv.Itoa(player2Hand.Wallet) + `";`)516 w.Eval(`document.getElementById("player2").getElementsByClassName("bet")[0].innerHTML = "` + strconv.Itoa(player2Hand.Bet) + `";`)517 w.Eval(`document.getElementById("player3").getElementsByTagName("p")[0].innerHTML = "` + player3Hand.ToStr() + `";`)518 w.Eval(`document.getElementById("player3").getElementsByClassName("wallet")[0].innerHTML = "` + strconv.Itoa(player3Hand.Wallet) + `";`)519 w.Eval(`document.getElementById("player3").getElementsByClassName("bet")[0].innerHTML = "` + strconv.Itoa(player3Hand.Bet) + `";`)520 })521 if player1Hand.IsBlackjack() {522 //w.Dialog(webview.DialogTypeAlert, 0, "Blackjack", "Blackjack!")523 if dealersHand.ScoreCard(0) >= 10 {524 w.Dispatch(func() {525 w.Eval(`document.getElementById("dealer").innerHTML = ` + dealersHand.ToStr() + `;`)526 })527 }528 if dealersHand.IsBlackjack() {529 //w.Dialog(webview.DialogTypeAlert, 0, "Blackjack", "Blackjack!The game is a push\n")530 } else {531 //player1Hand.Wallet+=player1Hand.Bet532 w.Eval(`document.getElementById("player1").getElementsByClassName("wallet")[0].innerHTML = "` + strconv.Itoa(player1Hand.Wallet) + `";`)533 }534 }535 if player2Hand.IsBlackjack() {536 //w.Dialog(webview.DialogTypeAlert, 0, "Blackjack", "Blackjack!")537 if dealersHand.ScoreCard(0) >= 10 {538 w.Dispatch(func() {539 w.Eval(`document.getElementById("dealer").innerHTML = ` + dealersHand.ToStr() + `;`)540 })541 }542 if dealersHand.IsBlackjack() {543 //w.Dialog(webview.DialogTypeAlert, 0, "Blackjack", "Blackjack!The game is a push\n")544 } else {545 //player2Hand.Wallet+=player2Hand.Bet546 w.Eval(`document.getElementById("player2").getElementsByClassName("wallet")[0].innerHTML = "` + strconv.Itoa(player1Hand.Wallet) + `";`)547 }548 }549 if player3Hand.IsBlackjack() {550 //w.Dialog(webview.DialogTypeAlert, 0, "Blackjack", "Blackjack!")551 if dealersHand.ScoreCard(0) >= 10 {552 w.Dispatch(func() {553 w.Eval(`document.getElementById("dealer").innerHTML = ` + dealersHand.ToStr() + `;`)554 })555 }556 if dealersHand.IsBlackjack() {557 //w.Dialog(webview.DialogTypeAlert, 0, "Blackjack", "Blackjack!The game is a push\n")558 } else {559 //player3Hand.Wallet+=player3Hand.Bet560 w.Eval(`document.getElementById("player3").getElementsByClassName("wallet")[0].innerHTML = "` + strconv.Itoa(player1Hand.Wallet) + `";`)561 }562 }563 w.Dispatch(func() {564 w.Eval(`document.getElementById("turn").innerHTML = "` + turn + `";`)565 w.Eval(`document.getElementById("start").style.visibility = "hidden";`)566 w.Eval(`document.getElementById("settle").style.visibility = "hidden";`)567 })568 } else {569 w.Dialog(webview.DialogTypeAlert, 0, "Game is not finished", "Game should be settled\n")570 }571}572func hit(w webview.WebView) {573 if turn == "player1's turn" {574 card := player1Hand.Hit()575 w.Dispatch(func() {576 w.Eval(`document.getElementById("player1").getElementsByTagName("p")[0].innerHTML += ",` + card.ToStr() + `";`)577 })578 if player1Hand.Score() > 21 {579 w.Dialog(webview.DialogTypeAlert, 0, "Player1", "Player1 bust!\n")580 player1Hand.IsBust = true581 if player1Hand.HasSplit {582 turn = "player1's Split turn"583 } else if player2Hand.Bet != 0 {584 turn = "player2's turn"585 } else if player3Hand.Bet != 0 {586 turn = "player3's turn"587 } else {588 turn = "dealer's turn"589 w.Eval(`document.getElementById("dealer").getElementsByTagName("p")[0].innerHTML += ",` + dealersHand.Cards[1].ToStr() + `";`)590 w.Eval(`document.getElementById("settle").style.visibility = "visible";`)591 w.Eval(`document.getElementById("hit").style.visibility = "hidden";`)592 w.Eval(`document.getElementById("stand").style.visibility = "hidden";`)593 w.Eval(`document.getElementById("double").style.visibility = "hidden";`)594 w.Eval(`document.getElementById("split").style.visibility = "hidden";`)595 }596 }597 w.Dispatch(func() {598 w.Eval(`document.getElementById("turn").innerHTML = "` + turn + `";`)599 })600 } else if turn == "player1's Split turn" {601 player1Split.Hit()602 w.Eval(`document.getElementById("player1").getElementsByClassName("splithand")[0].innerHTML = "` + player1Split.ToStr() + `";`)603 if player1Split.Score() > 21 {604 w.Dialog(webview.DialogTypeAlert, 0, "Player1", "Player1 Split bust!\n")605 player1Split.IsBust = true606 if player2Hand.Bet != 0 {607 turn = "player2's turn"608 } else if player3Hand.Bet != 0 {609 turn = "player3's turn"610 } else {611 turn = "dealer's turn"612 w.Eval(`document.getElementById("dealer").getElementsByTagName("p")[0].innerHTML += ",` + dealersHand.Cards[1].ToStr() + `";`)613 w.Eval(`document.getElementById("settle").style.visibility = "visible";`)614 w.Eval(`document.getElementById("hit").style.visibility = "hidden";`)615 w.Eval(`document.getElementById("stand").style.visibility = "hidden";`)616 w.Eval(`document.getElementById("double").style.visibility = "hidden";`)617 w.Eval(`document.getElementById("split").style.visibility = "hidden";`)618 }619 }620 w.Dispatch(func() {621 w.Eval(`document.getElementById("turn").innerHTML = "` + turn + `";`)622 })623 } else if turn == "player2's turn" {624 card := player2Hand.Hit()625 w.Dispatch(func() {626 w.Eval(`document.getElementById("player2").getElementsByTagName("p")[0].innerHTML += ",` + card.ToStr() + `";`)627 })628 if player2Hand.Score() > 21 {629 w.Dialog(webview.DialogTypeAlert, 0, "Player2", "Player2 bust!\n")630 player2Hand.IsBust = true631 if player1Hand.HasSplit {632 turn = "player2's Split turn"633 } else if player3Hand.Bet != 0 {634 turn = "player3's turn"635 } else {636 turn = "dealer's turn"637 w.Eval(`document.getElementById("dealer").getElementsByTagName("p")[0].innerHTML += ",` + dealersHand.Cards[1].ToStr() + `";`)638 w.Eval(`document.getElementById("settle").style.visibility = "visible";`)639 w.Eval(`document.getElementById("hit").style.visibility = "hidden";`)640 w.Eval(`document.getElementById("stand").style.visibility = "hidden";`)641 w.Eval(`document.getElementById("double").style.visibility = "hidden";`)642 w.Eval(`document.getElementById("split").style.visibility = "hidden";`)643 }644 }645 w.Dispatch(func() {646 w.Eval(`document.getElementById("turn").innerHTML = "` + turn + `";`)647 })648 } else if turn == "player2's Split turn" {649 player2Split.Hit()650 w.Eval(`document.getElementById("player2").getElementsByClassName("splithand")[0].innerHTML = "` + player2Split.ToStr() + `";`)651 if player2Split.Score() > 21 {652 w.Dialog(webview.DialogTypeAlert, 0, "Player2", "Player2 Split bust!\n")653 player2Split.IsBust = true654 if player3Hand.Bet != 0 {655 turn = "player3's turn"656 } else {657 turn = "dealer's turn"658 w.Eval(`document.getElementById("dealer").getElementsByTagName("p")[0].innerHTML += ",` + dealersHand.Cards[1].ToStr() + `";`)659 w.Eval(`document.getElementById("settle").style.visibility = "visible";`)660 w.Eval(`document.getElementById("hit").style.visibility = "hidden";`)661 w.Eval(`document.getElementById("stand").style.visibility = "hidden";`)662 w.Eval(`document.getElementById("double").style.visibility = "hidden";`)663 w.Eval(`document.getElementById("split").style.visibility = "hidden";`)664 }665 }666 w.Dispatch(func() {667 w.Eval(`document.getElementById("turn").innerHTML = "` + turn + `";`)668 })669 } else if turn == "player3's turn" {670 card := player3Hand.Hit()671 w.Dispatch(func() {672 w.Eval(`document.getElementById("player3").getElementsByTagName("p")[0].innerHTML += ",` + card.ToStr() + `";`)673 })674 if player3Hand.Score() > 21 {675 w.Dialog(webview.DialogTypeAlert, 0, "Player3", "Player3 bust!\n")676 w.Dispatch(func() {677 w.Eval(`document.getElementById("dealer").getElementsByTagName("p")[0].innerHTML += ",` + dealersHand.Cards[1].ToStr() + `";`)678 })679 player3Hand.IsBust = true680 if player3Hand.HasSplit {681 turn = "player3's Split turn"682 } else {683 turn = "dealer's turn"684 w.Eval(`document.getElementById("dealer").getElementsByTagName("p")[0].innerHTML += ",` + dealersHand.Cards[1].ToStr() + `";`)685 w.Eval(`document.getElementById("settle").style.visibility = "visible";`)686 w.Eval(`document.getElementById("hit").style.visibility = "hidden";`)687 w.Eval(`document.getElementById("stand").style.visibility = "hidden";`)688 w.Eval(`document.getElementById("double").style.visibility = "hidden";`)689 w.Eval(`document.getElementById("split").style.visibility = "hidden";`)690 }691 }692 w.Dispatch(func() {693 w.Eval(`document.getElementById("turn").innerHTML = "` + turn + `";`)694 })695 } else if turn == "player3's Split turn" {696 player3Split.Hit()697 w.Eval(`document.getElementById("player3").getElementsByClassName("splithand")[0].innerHTML = "` + player3Split.ToStr() + `";`)698 if player3Split.Score() > 21 {699 w.Dialog(webview.DialogTypeAlert, 0, "Player3", "Player3 Split bust!\n")700 player3Split.IsBust = true701 turn = "dealer's turn"702 w.Eval(`document.getElementById("dealer").getElementsByTagName("p")[0].innerHTML += ",` + dealersHand.Cards[1].ToStr() + `";`)703 w.Eval(`document.getElementById("settle").style.visibility = "visible";`)704 w.Eval(`document.getElementById("hit").style.visibility = "hidden";`)705 w.Eval(`document.getElementById("stand").style.visibility = "hidden";`)706 w.Eval(`document.getElementById("double").style.visibility = "hidden";`)707 w.Eval(`document.getElementById("split").style.visibility = "hidden";`)708 }709 w.Dispatch(func() {710 w.Eval(`document.getElementById("turn").innerHTML = "` + turn + `";`)711 })712 }713}714func stand(w webview.WebView) {715 if turn == "player1's turn" {716 if player1Hand.HasSplit {717 turn = "player1's Split turn"718 } else if player2Hand.Bet != 0 {719 turn = "player2's turn"720 } else if player3Hand.Bet != 0 {721 turn = "player3's turn"722 } else {723 turn = "dealer's turn"724 w.Eval(`document.getElementById("dealer").getElementsByTagName("p")[0].innerHTML += ",` + dealersHand.Cards[1].ToStr() + `";`)725 w.Eval(`document.getElementById("settle").style.visibility = "visible";`)726 w.Eval(`document.getElementById("hit").style.visibility = "hidden";`)727 w.Eval(`document.getElementById("stand").style.visibility = "hidden";`)728 w.Eval(`document.getElementById("double").style.visibility = "hidden";`)729 w.Eval(`document.getElementById("split").style.visibility = "hidden";`)730 }731 w.Dispatch(func() {732 w.Eval(`document.getElementById("turn").innerHTML = "` + turn + `";`)733 })734 } else if turn == "player1's Split turn" {735 if player2Hand.Bet != 0 {736 turn = "player2's turn"737 } else if player3Hand.Bet != 0 {738 turn = "player3's turn"739 } else {740 turn = "dealer's turn"741 w.Eval(`document.getElementById("dealer").getElementsByTagName("p")[0].innerHTML += ",` + dealersHand.Cards[1].ToStr() + `";`)742 w.Eval(`document.getElementById("settle").style.visibility = "visible";`)743 w.Eval(`document.getElementById("hit").style.visibility = "hidden";`)744 w.Eval(`document.getElementById("stand").style.visibility = "hidden";`)745 w.Eval(`document.getElementById("double").style.visibility = "hidden";`)746 w.Eval(`document.getElementById("split").style.visibility = "hidden";`)747 }748 w.Dispatch(func() {749 w.Eval(`document.getElementById("turn").innerHTML = "` + turn + `";`)750 })751 } else if turn == "player2's turn" {752 if player2Hand.HasSplit {753 turn = "player2's Split turn"754 } else if player3Hand.Bet != 0 {755 turn = "player3's turn"756 } else {757 turn = "dealer's turn"758 w.Eval(`document.getElementById("dealer").getElementsByTagName("p")[0].innerHTML += ",` + dealersHand.Cards[1].ToStr() + `";`)759 w.Eval(`document.getElementById("settle").style.visibility = "visible";`)760 w.Eval(`document.getElementById("hit").style.visibility = "hidden";`)761 w.Eval(`document.getElementById("stand").style.visibility = "hidden";`)762 w.Eval(`document.getElementById("double").style.visibility = "hidden";`)763 }764 w.Dispatch(func() {765 w.Eval(`document.getElementById("turn").innerHTML = "` + turn + `";`)766 })767 } else if turn == "player2's Split turn" {768 if player3Hand.Bet != 0 {769 turn = "player3's turn"770 } else {771 turn = "dealer's turn"772 w.Eval(`document.getElementById("dealer").getElementsByTagName("p")[0].innerHTML += ",` + dealersHand.Cards[1].ToStr() + `";`)773 w.Eval(`document.getElementById("settle").style.visibility = "visible";`)774 w.Eval(`document.getElementById("hit").style.visibility = "hidden";`)775 w.Eval(`document.getElementById("stand").style.visibility = "hidden";`)776 w.Eval(`document.getElementById("double").style.visibility = "hidden";`)777 }778 w.Dispatch(func() {779 w.Eval(`document.getElementById("turn").innerHTML = "` + turn + `";`)780 })781 } else if turn == "player3's turn" {782 if player1Hand.HasSplit {783 turn = "player3's Split turn"784 } else {785 turn = "dealer's turn"786 w.Eval(`document.getElementById("settle").style.visibility = "visible";`)787 w.Eval(`document.getElementById("hit").style.visibility = "hidden";`)788 w.Eval(`document.getElementById("stand").style.visibility = "hidden";`)789 w.Eval(`document.getElementById("double").style.visibility = "hidden";`)790 w.Eval(`document.getElementById("split").style.visibility = "hidden";`)791 w.Dispatch(func() {792 w.Eval(`document.getElementById("dealer").getElementsByTagName("p")[0].innerHTML += ",` + dealersHand.Cards[1].ToStr() + `";`)793 w.Eval(`document.getElementById("turn").innerHTML = "` + turn + `";`)794 })795 }796 } else if turn == "player3's Split turn" {797 turn = "dealer's turn"798 w.Eval(`document.getElementById("settle").style.visibility = "visible";`)799 w.Eval(`document.getElementById("hit").style.visibility = "hidden";`)800 w.Eval(`document.getElementById("stand").style.visibility = "hidden";`)801 w.Eval(`document.getElementById("double").style.visibility = "hidden";`)802 w.Eval(`document.getElementById("split").style.visibility = "hidden";`)803 w.Dispatch(func() {804 w.Eval(`document.getElementById("dealer").getElementsByTagName("p")[0].innerHTML += ",` + dealersHand.Cards[1].ToStr() + `";`)805 w.Eval(`document.getElementById("turn").innerHTML = "` + turn + `";`)806 })807 }808}809func split(w webview.WebView) {810 if turn == "player1's turn" {811 if player1Hand.CanSplit() {812 player1Split = player1Hand.Split()813 player1Hand.HasSplit = true814 w.Eval(`document.getElementById("player1").getElementsByClassName("splithand")[0].style.visibility = "visible";`)815 w.Dispatch(func() {816 w.Eval(`document.getElementById("turn").innerHTML = "` + turn + `";`)817 w.Eval(`document.getElementById("player1").getElementsByTagName("p")[0].innerHTML = "` + player1Hand.ToStr() + `";`)818 w.Eval(`document.getElementById("player1").getElementsByClassName("wallet")[0].innerHTML = "` + strconv.Itoa(player1Hand.Wallet) + `";`)819 w.Eval(`document.getElementById("player1").getElementsByClassName("bet")[0].innerHTML = "` + strconv.Itoa(player1Hand.Bet) + `";`)820 w.Eval(`document.getElementById("player1").getElementsByClassName("splithand")[0].innerHTML = "` + player1Split.ToStr() + `";`)821 })822 } else {823 w.Dialog(webview.DialogTypeAlert, 0, "Player1", "Player1 can not split!\n")824 }825 } else if turn == "player2's turn" {826 if player2Hand.CanSplit() {827 player2Split = player2Hand.Split()828 player2Hand.HasSplit = true829 w.Eval(`document.getElementById("player2").getElementsByClassName("splithand")[0].style.visibility = "visible";`)830 w.Dispatch(func() {831 w.Eval(`document.getElementById("turn").innerHTML = "` + turn + `";`)832 w.Eval(`document.getElementById("player2").getElementsByTagName("p")[0].innerHTML = "` + player2Hand.ToStr() + `";`)833 w.Eval(`document.getElementById("player2").getElementsByClassName("wallet")[0].innerHTML = "` + strconv.Itoa(player2Hand.Wallet) + `";`)834 w.Eval(`document.getElementById("player2").getElementsByClassName("bet")[0].innerHTML = "` + strconv.Itoa(player2Hand.Bet) + `";`)835 w.Eval(`document.getElementById("player2").getElementsByClassName("splithand")[0].innerHTML = "` + player2Split.ToStr() + `";`)836 })837 } else {838 w.Dialog(webview.DialogTypeAlert, 0, "Player2", "Player2 can not split!\n")839 }840 } else if turn == "player3's turn" {841 if player3Hand.CanSplit() {842 player3Split = player3Hand.Split()843 player3Hand.HasSplit = true844 w.Eval(`document.getElementById("player3").getElementsByClassName("splithand")[0].style.visibility = "visible";`)845 w.Dispatch(func() {846 w.Eval(`document.getElementById("turn").innerHTML = "` + turn + `";`)847 w.Eval(`document.getElementById("player3").getElementsByTagName("p")[0].innerHTML = "` + player3Hand.ToStr() + `";`)848 w.Eval(`document.getElementById("player3").getElementsByClassName("wallet")[0].innerHTML = "` + strconv.Itoa(player3Hand.Wallet) + `";`)849 w.Eval(`document.getElementById("player3").getElementsByClassName("bet")[0].innerHTML = "` + strconv.Itoa(player3Hand.Bet) + `";`)850 w.Eval(`document.getElementById("player3").getElementsByClassName("splithand")[0].innerHTML = "` + player3Split.ToStr() + `";`)851 })852 } else {853 w.Dialog(webview.DialogTypeAlert, 0, "Player3", "Player3 can not split!\n")854 }855 } else {856 w.Dialog(webview.DialogTypeAlert, 0, "Players", "Can not split twice!\n")857 }858}...
html2md.go
Source:html2md.go
1// a go port of html2md javascript version2package html2md3import (4 "fmt"5 "regexp"6 "strconv"7 "strings"8)9func P() *Rule {10 return &Rule{11 Patterns: []string{"p"},12 Replacement: func(innerHTML string, attrs []string) string {13 if len(attrs) > 1 {14 return "\n\n" + attrs[1] + "\n"15 }16 return ""17 },18 }19}20func Br() *Rule {21 return &Rule{22 Patterns: []string{"br"},23 Tp: Void,24 Replacement: func(innerHTML string, attrs []string) string {25 return " \n"26 },27 }28}29func H() *Rule {30 return &Rule{31 Patterns: []string{"h([1-6])"},32 Replacement: func(innerHTML string, attrs []string) string {33 if len(attrs) < 4 || attrs[0] != attrs[len(attrs)-1] {34 return ""35 }36 hLevel, err := strconv.Atoi(attrs[0])37 if err != nil {38 fmt.Println(err)39 return ""40 }41 return "\n\n" + strings.Repeat("#", hLevel) +42 " " + strings.Replace(strings.Replace(attrs[2], "\n", " ", -1), "\r", " ", -1) + "\n"43 },44 }45}46func Hr() *Rule {47 return &Rule{48 Patterns: []string{"hr"},49 Tp: Void,50 Replacement: func(innerHTML string, attrs []string) string {51 return "\n\n* * *\n"52 },53 }54}55func B() *Rule {56 return &Rule{57 Patterns: []string{"b", "strong"},58 Replacement: func(innerHTML string, attrs []string) string {59 if len(attrs) > 1 {60 return wrapInlineTag(attrs[1], "**", "**")61 }62 return ""63 },64 }65}66func I() *Rule {67 return &Rule{68 Patterns: []string{"i", "em"},69 Replacement: func(innerHTML string, attrs []string) string {70 if len(attrs) > 1 {71 return wrapInlineTag(attrs[1], "_", "_")72 }73 return ""74 },75 }76}77func Code() *Rule {78 return &Rule{79 Patterns: []string{"code", "tt", "pre"},80 Replacement: func(innerHTML string, attrs []string) string {81 if len(attrs) > 1 {82 return "`" + attrs[1] + "`"83 }84 return ""85 },86 }87}88func A() *Rule {89 return &Rule{90 Patterns: []string{"a"},91 Replacement: func(innerHTML string, attrs []string) string {92 var href string93 hrefR := AttrRegExp("href")94 matches := hrefR.FindStringSubmatch(attrs[0])95 if len(matches) > 1 {96 href = matches[1]97 }98 /*targetR := AttrRegExp("target")99 matches = targetR.FindStringSubmatch(attrs[0])100 if len(matches) > 1 {101 target = matches[1]102 }*/103 //if len(target) > 0 {104 // return "[" + alt + "]" + "(" + src + " \"" + title + "\")"105 //}106 return wrapInlineTag(attrs[1], "[", "]") + "(" + href + ")"107 },108 }109}110func SameRule(tag string, tp int) *Rule {111 return &Rule{Patterns: []string{tag},112 Tp: tp,113 Replacement: func(innerHTML string, attrs []string) string {114 return innerHTML115 },116 }117}118func Img() *Rule {119 return &Rule{120 Patterns: []string{"img"},121 Tp: Void,122 Replacement: func(innerHTML string, attrs []string) string {123 var src, alt, title string124 srcR := AttrRegExp("src")125 matches := srcR.FindStringSubmatch(attrs[0])126 if len(matches) > 1 {127 src = matches[1]128 }129 altR := AttrRegExp("alt")130 matches = altR.FindStringSubmatch(attrs[0])131 if len(matches) > 1 {132 alt = matches[1]133 }134 titleR := AttrRegExp("title")135 matches = titleR.FindStringSubmatch(attrs[0])136 if len(matches) > 1 {137 title = matches[1]138 }139 if len(title) > 0 {140 if len(alt) == 0 {141 alt = title142 }143 return "![" + alt + "]" + "(" + src + " \"" + title + "\")"144 }145 if len(alt) == 0 {146 alt = "image"147 }148 return "![" + alt + "]" + "(" + src + ")"149 },150 }151}152func replaceEls(html, tag string, tp int, replacement ReplaceFunc) string {153 var pattern string154 if tp == Void {155 pattern = "<" + tag + "\\b([^>]*)\\/?>"156 } else {157 pattern = "<" + tag + "\\b([^>]*)>([\\s\\S]*?)<\\/" + tag + ">"158 }159 re := regexp.MustCompile(pattern)160 return re.ReplaceAllStringFunc(html, func(subHtml string) string {161 matches := re.FindStringSubmatch(subHtml)162 //fmt.Println("xx", subHtml, matches)163 return replacement(subHtml, matches[1:])164 })165}166func replaceLists(tag, html string) string {167 re := regexp.MustCompile(`<(` + tag + `)\b[^>]*>([\s\S]*?)</` + tag + `>`)168 html = re.ReplaceAllStringFunc(html, func(innerHTML string) string {169 var lis = strings.Split(innerHTML, "</li>")170 var newLis = make([]string, 0)171 var prefix string = "* "172 for i, li := range lis[:len(lis)-1] {173 if tag == "ol" {174 prefix = fmt.Sprintf("%d. ", i+1)175 }176 re := regexp.MustCompile(`([\s\S]*)<li[^>]*>([\s\S]*)`)177 newLis = append(newLis, re.ReplaceAllStringFunc(li, func(innerHTML string) string {178 matches := re.FindStringSubmatch(innerHTML)179 innerHTML = regexp.MustCompile(`/^\s+/`).ReplaceAllString(matches[2], "")180 innerHTML = regexp.MustCompile(`/\n\n/g`).ReplaceAllString(innerHTML, "\n\n ")181 // indent nested lists182 innerHTML = regexp.MustCompile(`/\n([ ]*)+(\*|\d+\.) /g`).ReplaceAllString(innerHTML, "\n$1 $2 ")183 return prefix + innerHTML184 }))185 }186 return strings.Join(newLis, "\n")187 })188 return "\n\n" + regexp.MustCompile(`[ \t]+\n|\s+$`).ReplaceAllString(html, "\n")189}190func replaceBlockquotes(html string) string {191 re := regexp.MustCompile(`<blockquote\b[^>]*>([\s\S]*?)</blockquote>`)192 return re.ReplaceAllStringFunc(html, func(inner string) string {193 matches := re.FindStringSubmatch(inner)194 inner = regexp.MustCompile(`^\s+|\s+$`).ReplaceAllString(matches[1], "")195 inner = cleanUp(inner)196 inner = regexp.MustCompile(`(?m)^`).ReplaceAllString(inner, "> ")197 inner = regexp.MustCompile(`^(>([ \t]{2,}>)+)`).ReplaceAllString(inner, "> >")198 return inner199 })200}201func blockQuote(content string) string {202 // Blockquotes203 //var deepest = `<blockquote\b[^>]*>((?:(?!<blockquote)[\s\S])*?)</blockquote>`204 var deepest = `<blockquote\b[^>]*>((?:[\s\S])*?)</blockquote>`205 re := regexp.MustCompile(deepest)206 content = re.ReplaceAllStringFunc(content, func(str string) string {207 return replaceBlockquotes(str)208 })209 return content210}211func Remove(ct, tag string) string {212 re := regexp.MustCompile("\\<" + tag + "[\\S\\s]+?\\</" + tag + "\\>")213 return re.ReplaceAllString(ct, "")214}215func cleanUp(ct string) string {216 // trim leading/trailing whitespace217 str := regexp.MustCompile("^[\t\r\n]+|[\t\r\n]+$").ReplaceAllString(ct, "")218 str = regexp.MustCompile(`\n\s+\n`).ReplaceAllString(str, "\n\n")219 // limit consecutive linebreaks to 2220 str = regexp.MustCompile(`\n{3,}`).ReplaceAllString(str, "\n\n")221 //å»é¤STYLE222 str = Remove(str, "style")223 //å»é¤SCRIPT224 str = Remove(str, "script")225 //å»é¤ææå°æ¬å·å
çHTML代ç ï¼å¹¶æ¢ææ¢è¡ç¬¦226 re := regexp.MustCompile("\\<[\\S\\s]+?\\>")227 str = re.ReplaceAllString(str, "\n")228 //å»é¤è¿ç»çæ¢è¡ç¬¦229 //re = regexp.MustCompile("\\s{2,}")230 //str = re.ReplaceAllString(str, "\n")231 return str232}233func pre(content string) string {234 // Pre code blocks235 re := regexp.MustCompile(`<pre\b[^>]*>([\s\S]*)</pre>`)236 content = re.ReplaceAllStringFunc(content, func(innerHTML string) string {237 matches := re.FindStringSubmatch(innerHTML)238 // convert tabs to spaces (you know it makes sense)239 r := regexp.MustCompile(`/^\t+`)240 innerHTML = r.ReplaceAllString(matches[1], " ")241 r = regexp.MustCompile(`/\n`)242 innerHTML = r.ReplaceAllString(innerHTML, "\n ")243 return "\n\n " + innerHTML + "\n"244 })245 return content246}247func ul(content string) string {248 return ulol("ul", content)249}250func ol(content string) string {251 return ulol("ol", content)252}253func ulol(tag, content string) string {254 // Lists255 // Escape numbers that could trigger an ol256 // If there are more than three spaces before the code, it would be in a pre tag257 // Make sure we are escaping the period not matching any character258 //content = string.replace(^(\s{0,3}\d+)\. /g, "$1\\. ");259 // Converts lists that have no child lists (of same type) first, then works it"s way up260 //var noChildrenRegex = /<(ul|ol)\b[^>]*>(?:(?!<ul|<ol)[\s\S])*?<\/\1>/gi;261 var noChildrenRegex = `<(` + tag + `)\b[^>]*>(?:[\s\S])*?</` + tag + `>`262 re := regexp.MustCompile(noChildrenRegex)263 return re.ReplaceAllStringFunc(content, func(str string) string {264 return replaceLists(tag, str)265 })266}267func wrapInlineTag(content, openWrap, closeWrap string) string {268 wrappedStr := openWrap + strings.TrimSpace(content) + closeWrap269 if regexp.MustCompile(`^\s.*`).MatchString(content) {270 wrappedStr = " " + wrappedStr271 }272 if regexp.MustCompile(`.*\s$`).MatchString(content) {273 wrappedStr = wrappedStr + " "274 }275 return wrappedStr276}277func WrapInlineTag(content, openWrap, closeWrap string) string {278 return wrapInlineTag(content, openWrap, closeWrap)279}280func init() {281 AddRule("p", P())282 AddRule("i", I())283 AddRule("h", H())284 AddRule("hr", Hr())285 AddRule("img", Img())286 AddRule("b", B())287 AddRule("br", Br())288 AddRule("code", Code())289 AddRule("a", A())290 AddConvert(pre)291 AddConvert(ul)292 AddConvert(ol)293 AddConvert(blockQuote)294 AddConvert(cleanUp)295}296func Convert(content string) string {297 for _, rule := range rules {298 for _, pattern := range rule.Patterns {299 content = replaceEls(content, pattern, rule.Tp, rule.Replacement)300 }301 }302 for _, convert := range converts {303 content = convert(content)304 }305 return content306}...
InnerHTML
Using AI Code Generation
1import (2func main() {3 vm := otto.New()4 vm.Run(`5 var html = document.createElement('html');6 var body = document.createElement('body');7 var div = document.createElement('div');8 div.innerHTML = '<p>Hello World!</p>';9 body.appendChild(div);10 html.appendChild(body);11 div.innerHTML;12 html, _ := vm.Get("html")13 body, _ := vm.Get("body")14 div, _ := vm.Get("div")15 fmt.Println(html)16 fmt.Println(body)17 fmt.Println(div)18}
InnerHTML
Using AI Code Generation
1import (2func main() {3 response, err := http.Get(url)4 if err != nil {5 fmt.Println(err)6 }7 defer response.Body.Close()8 contents, err := ioutil.ReadAll(response.Body)9 if err != nil {10 fmt.Println(err)11 }12 doc, err := goquery.NewDocumentFromReader(strings.NewReader(string(contents)))13 if err != nil {14 fmt.Println(err)15 }16 doc.Find("div#content").Each(func(index int, item *goquery.Selection) {17 fmt.Println(item.Find("h2").Text())18 })19}
InnerHTML
Using AI Code Generation
1import "fmt"2import "github.com/antchfx/htmlquery"3func main() {4 if err != nil {5 fmt.Println(err)6 }7 fmt.Println(htmlquery.InnerText(node))8}
InnerHTML
Using AI Code Generation
1import "fmt"2import "net/http"3import "io/ioutil"4import "strings"5import "strconv"6import "encoding/json"7import "github.com/PuerkitoBio/goquery"8type Html struct {9}10func (h Html) InnerHTML() string {11 resp, err := http.Get(h.Url)12 if err != nil {13 }14 defer resp.Body.Close()15 body, err := ioutil.ReadAll(resp.Body)16 if err != nil {17 }18 return string(body)19}20func main() {21 doc, _ := goquery.NewDocumentFromReader(strings.NewReader(h.InnerHTML()))22 doc.Find("div#search").Each(func(i int, s *goquery.Selection) {23 band := s.Find("h3.r a").Text()24 title := s.Find("div.s div span.st").Text()25 fmt.Printf("Review %d: %s - %s26 })27}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!