Skip to content

Commit

Permalink
create markdown parser
Browse files Browse the repository at this point in the history
  • Loading branch information
codemaestro64 committed Dec 31, 2020
1 parent 4e7d835 commit 4652261
Show file tree
Hide file tree
Showing 6 changed files with 626 additions and 108 deletions.
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ require (
github.com/gomarkdown/markdown v0.0.0-20201113031856-722100d81a8e
github.com/jessevdk/go-flags v1.4.1-0.20200711081900-c17162fe8fd7
github.com/jrick/logrotate v1.0.0
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/onsi/ginkgo v1.14.0
github.com/onsi/gomega v1.10.1
github.com/planetdecred/dcrlibwallet v1.5.3-0.20201113035912-7786819ad8aa
github.com/russross/blackfriday v2.0.0+incompatible
github.com/shurcooL/go v0.0.0-20200502201357-93f07166e636 // indirect
github.com/shurcooL/markdownfmt v0.0.0-20200725144734-77d690767c81
github.com/skip2/go-qrcode v0.0.0-20191027152451-9434209cb086
gitlab.com/golang-commonmark/markdown v0.0.0-20191127184510-91b5b3c99c19
golang.org/x/exp v0.0.0-20191002040644-a1355ae1e2c3
golang.org/x/image v0.0.0-20200618115811-c13761719519
golang.org/x/net v0.0.0-20200813134508-3edf25e44fcc
Expand Down
23 changes: 23 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,8 @@ github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+
github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/marcopeereboom/sbox v1.0.0/go.mod h1:V9e7t7oKphNfXymk7Lqvbo9mZiVjmCt8vBHnROcpCSY=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
Expand Down Expand Up @@ -549,7 +551,17 @@ github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqn
github.com/robertkrimen/otto v0.0.0-20180617131154-15f95af6e78d/go.mod h1:xvqspoSXJTIpemEonrMDFq6XzwHYYgToXWj5eRX1OtY=
github.com/robfig/cron v1.2.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/russross/blackfriday v1.5.2 h1:HyvC0ARfnZBqnXwABFeSZHpKvJHJJfPz81GNueLj0oo=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/russross/blackfriday v1.5.3-0.20190124082335-a477dd164691/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/russross/blackfriday v2.0.0+incompatible h1:cBXrhZNUf9C+La9/YpS+UHpUT8YD6Td9ZMSU9APFcsk=
github.com/russross/blackfriday v2.0.0+incompatible/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/shurcooL/go v0.0.0-20200502201357-93f07166e636 h1:aSISeOcal5irEhJd1M+IrApc0PdcN7e7Aj4yuEnOrfQ=
github.com/shurcooL/go v0.0.0-20200502201357-93f07166e636/go.mod h1:TDJrrUr11Vxrven61rcy3hJMUqaf/CLWYhHNPmT14Lk=
github.com/shurcooL/markdownfmt v0.0.0-20200725144734-77d690767c81 h1:fZK+IllxGmcRPu9ce7Q0dC18jMhja6vGHpb+zklGaUw=
github.com/shurcooL/markdownfmt v0.0.0-20200725144734-77d690767c81/go.mod h1:wwC6+1FOCAA/hK8+pmBir20vneHxr8Nh0OGQNkyo2a8=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/skip2/go-qrcode v0.0.0-20191027152451-9434209cb086 h1:RYiqpb2ii2Z6J4x0wxK46kvPBbFuZcdhS+CIztmYgZs=
github.com/skip2/go-qrcode v0.0.0-20191027152451-9434209cb086/go.mod h1:PLPIyL7ikehBD1OAjmKKiOEhbvWyHGaNDjquXMcYABo=
Expand All @@ -576,6 +588,17 @@ github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljT
github.com/vmihailenco/msgpack v4.0.1+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
gitlab.com/golang-commonmark/html v0.0.0-20191124015941-a22733972181 h1:K+bMSIx9A7mLES1rtG+qKduLIXq40DAzYHtb0XuCukA=
gitlab.com/golang-commonmark/html v0.0.0-20191124015941-a22733972181/go.mod h1:dzYhVIwWCtzPAa4QP98wfB9+mzt33MSmM8wsKiMi2ow=
gitlab.com/golang-commonmark/linkify v0.0.0-20191026162114-a0c2df6c8f82 h1:oYrL81N608MLZhma3ruL8qTM4xcpYECGut8KSxRY59g=
gitlab.com/golang-commonmark/linkify v0.0.0-20191026162114-a0c2df6c8f82/go.mod h1:Gn+LZmCrhPECMD3SOKlE+BOHwhOYD9j7WT9NUtkCrC8=
gitlab.com/golang-commonmark/markdown v0.0.0-20191127184510-91b5b3c99c19 h1:HsZm6XaTpEgZiZqcXZkUbG6BNtSZE3XyCTfo52YBoDY=
gitlab.com/golang-commonmark/markdown v0.0.0-20191127184510-91b5b3c99c19/go.mod h1:CRIzp0wh6PvKEAeEOtp9wEpNKJJ1VFTNfHO4+ToRgVA=
gitlab.com/golang-commonmark/mdurl v0.0.0-20191124015652-932350d1cb84 h1:qqjvoVXdWIcZCLPMlzgA7P9FZWdPGPvP/l3ef8GzV6o=
gitlab.com/golang-commonmark/mdurl v0.0.0-20191124015652-932350d1cb84/go.mod h1:IJZ+fdMvbW2qW6htJx7sLJ04FEs4Ldl/MDsJtMKywfw=
gitlab.com/golang-commonmark/puny v0.0.0-20191124015043-9f83538fa04f h1:Wku8eEdeJqIOFHtrfkYUByc4bCaTeA6fL0UJgfEiFMI=
gitlab.com/golang-commonmark/puny v0.0.0-20191124015043-9f83538fa04f/go.mod h1:Tiuhl+njh/JIg0uS/sOJVYi0x2HEa5rc1OAaVsb5tAs=
gitlab.com/opennota/wd v0.0.0-20180912061657-c5d65f63c638/go.mod h1:EGRJaqe2eO9XGmFtQCvV3Lm9NLico3UhFwUpCG/+mVU=
go.etcd.io/bbolt v1.3.0/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.etcd.io/bbolt v1.3.3 h1:MUGmc65QhB3pIlaQ5bB4LwqSj6GIonVJXpZiaKNyaKk=
go.etcd.io/bbolt v1.3.3/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
Expand Down
10 changes: 9 additions & 1 deletion ui/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ const (
navDrawerMinimizedWidth = 100
)

var (
appBarWidth = 0
)

func (win *Window) addPages(decredIcons map[string]image.Image) {
ic := pageIcons{
contentAdd: mustIcon(widget.NewIcon(icons.ContentAdd)),
Expand Down Expand Up @@ -257,6 +261,7 @@ func (page pageCommon) Layout(gtx layout.Context, body layout.Widget) layout.Dim
width = navDrawerMinimizedWidth
}
gtx.Constraints.Max.X = width
gtx.Constraints.Min.X = width
return decredmaterial.Card{Color: page.theme.Color.Surface}.Layout(gtx, func(gtx C) D {
page.layoutNavDrawer(gtx)
return layout.Dimensions{Size: gtx.Constraints.Max}
Expand All @@ -273,7 +278,7 @@ func (page pageCommon) Layout(gtx layout.Context, body layout.Widget) layout.Dim
}

func (page pageCommon) layoutAppBar(gtx layout.Context) layout.Dimensions {
return decredmaterial.Card{Color: page.theme.Color.Surface}.Layout(gtx, func(gtx C) D {
dims := decredmaterial.Card{Color: page.theme.Color.Surface}.Layout(gtx, func(gtx C) D {
gtx.Constraints.Min.X = gtx.Constraints.Max.X
return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(func(gtx C) D {
Expand Down Expand Up @@ -340,6 +345,9 @@ func (page pageCommon) layoutAppBar(gtx layout.Context) layout.Dimensions {
}),
)
})
appBarWidth = dims.Size.X

return dims
}

func (page pageCommon) layoutNavDrawer(gtx layout.Context) layout.Dimensions {
Expand Down
117 changes: 27 additions & 90 deletions ui/proposal_details_page.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
package ui

import (
//"bytes"
"encoding/base64"
"fmt"
"os/exec"
"regexp"
//"regexp"
"runtime"
"strings"
//"strings"

"gioui.org/layout"
"gioui.org/unit"
"gioui.org/widget"
"gioui.org/widget/material"
//"gioui.org/widget/material"

//"github.com/PuerkitoBio/goquery"
//"gitlab.com/golang-commonmark/markdown"
//"github.com/gomarkdown/markdown"
//"github.com/gomarkdown/markdown/parser"
"github.com/planetdecred/dcrlibwallet"
"github.com/planetdecred/godcr/ui/decredmaterial"
"github.com/planetdecred/godcr/ui/utils"
"github.com/planetdecred/godcr/ui/values"
)

const PageProposalDetails = "proposaldetails"

type renderFunc func(C, []string) layout.Widget

type ProposalPage struct {
theme *decredmaterial.Theme
proposal **dcrlibwallet.Proposal
Expand All @@ -31,9 +39,17 @@ type ProposalPage struct {
container *layout.List
}

const (
//markdownLinkPlaceholder = "[[link]]"
//markdownHeadingPlaceholder = "[[heading--"

spacer = "@@@@"
linkTag = "[[link"
headingTag = "[[heading"
)

var (
markdownRegex = regexp.MustCompile(`\[[^][]+]\((https?://[^()]+)\)`)
markdownLinkPlaceholder = "[[link]]"
tags = []string{"li", "ul", "td", "tr", "th", "table", "strong", "p", "h1", "h2", "h3", "h4", "h5", "h6"}
)

func (win *Window) ProposalPage(common pageCommon) layout.Widget {
Expand Down Expand Up @@ -104,7 +120,6 @@ func (pg *ProposalPage) Layout(gtx layout.Context, c pageCommon) layout.Dimensio

func (pg *ProposalPage) layoutProposalDescription(gtx layout.Context) layout.Dimensions {
proposal := *pg.proposal

w := []layout.Widget{
func(gtx C) D {
return pg.layoutProposalHeader(gtx, false)
Expand Down Expand Up @@ -136,9 +151,8 @@ func (pg *ProposalPage) layoutProposalDescription(gtx layout.Context) layout.Dim
},
}

ws := pg.getProposalDescriptionTextParts(gtx)
w = append(w, ws...)

r := utils.RenderMarkdown(gtx, pg.theme, pg.getProposalText())
w = append(w, r.Layout()...)
return pg.container.Layout(gtx, len(w), func(gtx C, i int) D {
return layout.UniformInset(unit.Dp(0)).Layout(gtx, w[i])
})
Expand All @@ -164,7 +178,6 @@ func (pg *ProposalPage) layoutProposalHeader(gtx layout.Context, truncateTitle b
return getSubtitleLabel(pg.theme, fmt.Sprintf("Last updated %s", timeAgo(proposal.Timestamp))).Layout(gtx)
})
}

return layout.Dimensions{}
}),
)
Expand Down Expand Up @@ -203,7 +216,9 @@ func (pg *ProposalPage) layoutProposalDetailsSubHeaderRow(gtx layout.Context, le
)
}

func (pg *ProposalPage) getProposalDescriptionTextParts(gtx layout.Context) []layout.Widget {
var has = false

func (pg *ProposalPage) getProposalText() []byte {
proposal := *pg.proposal

var desc []byte
Expand All @@ -214,83 +229,5 @@ func (pg *ProposalPage) getProposalDescriptionTextParts(gtx layout.Context) []la
}
}

return pg.PrepareText(gtx, string(desc))
}

func (pg *ProposalPage) PrepareText(gtx layout.Context, text string) []layout.Widget {
// first extract all links and replace them with a placeholder
ls := markdownRegex.FindAllStringSubmatch(text, -1)
links := make([]string, len(ls))
for i := range ls {
index := i
links[index] = ls[index][0]
}

// replace all links with placeholder
txt := markdownRegex.ReplaceAllString(text, markdownLinkPlaceholder)

prevRune := ""
paragraphs := strings.FieldsFunc(txt, func(r rune) bool {
match := string(r) == "\n" && prevRune == "\n"
prevRune = string(r)

return match
})

w := []layout.Widget{}
for i := range paragraphs {
index := i
words := strings.Split(paragraphs[index], " ")
w = append(w, func(gtx C) D {
dims := decredmaterial.GridWrap{
Axis: layout.Horizontal,
Alignment: layout.End,
}.Layout(gtx, len(words), func(gtx C, i int) D {
word := words[i]
if words[i] == markdownLinkPlaceholder || strings.HasPrefix(words[i], markdownLinkPlaceholder) {
link := links[0]
links = links[1:]
word := strings.Replace(words[i], markdownLinkPlaceholder, link, -1)
return pg.layoutLinkWord(gtx, word)
}
return pg.theme.Body2(strings.TrimSpace(word) + " ").Layout(gtx)
})
dims.Size.Y += 20
return dims
})
}
return w
}

func (pg *ProposalPage) layoutLinkWord(gtx layout.Context, link string) layout.Dimensions {
text, _ := getStringInBetweenTwoString(link, "[", "]")
linkRef, _ := getStringInBetweenTwoString(link, "(", ")")

if pg.clickables == nil {
pg.clickables = map[string]*widget.Clickable{}
}

if _, ok := pg.clickables[linkRef]; !ok {
pg.clickables[linkRef] = new(widget.Clickable)
}

return material.Clickable(gtx, pg.clickables[linkRef], func(gtx C) D {
lbl := pg.theme.Body2(text + " ")
lbl.Color = pg.theme.Color.Primary
return lbl.Layout(gtx)
})
}

func getStringInBetweenTwoString(str string, startS string, endS string) (result string, found bool) {
s := strings.Index(str, startS)
if s == -1 {
return result, false
}
newS := str[s+len(startS):]
e := strings.Index(newS, endS)
if e == -1 {
return result, false
}
result = newS[:e]
return result, true
return desc
}
25 changes: 8 additions & 17 deletions ui/transaction_details_page.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package ui

import (
//"fmt"
"fmt"
"image/color"
//"os/exec"
//"runtime"
//"strings"
"os/exec"
"runtime"
"strings"

"gioui.org/layout"
//"gioui.org/text"
"gioui.org/text"
"gioui.org/widget"

//"github.com/decred/dcrd/dcrutil"
//"github.com/planetdecred/dcrlibwallet"
"github.com/decred/dcrd/dcrutil"
"github.com/planetdecred/dcrlibwallet"
"github.com/planetdecred/godcr/ui/decredmaterial"
"github.com/planetdecred/godcr/ui/values"
"github.com/planetdecred/godcr/wallet"
Expand Down Expand Up @@ -93,15 +93,6 @@ func (win *Window) TransactionDetailsPage(common pageCommon) layout.Widget {
}
}

func (pg *transactionDetailsPage) Layout(gtx layout.Context, common pageCommon) layout.Dimensions {
return layout.Dimensions{}
}

func (pg *transactionDetailsPage) Handler(common pageCommon) {

}

/**
func (pg *transactionDetailsPage) Layout(gtx layout.Context, common pageCommon) layout.Dimensions {
widgets := []func(gtx C) D{
func(gtx C) D {
Expand Down Expand Up @@ -581,4 +572,4 @@ func (pg *transactionDetailsPage) Handler(common pageCommon) {
common.clipboard <- WriteClipboard{Text: (*pg.txnInfo).Txn.Hash}
}()
}
}**/
}
Loading

0 comments on commit 4652261

Please sign in to comment.