feat: parse answer author and timestamp
This commit is contained in:
parent
010bd290af
commit
85dad1cbe0
46
main.go
46
main.go
@ -20,6 +20,10 @@ func main() {
|
||||
r.LoadHTMLGlob("templates/*")
|
||||
r.Static("/static", "./public")
|
||||
|
||||
r.GET("/robots.txt", func(c *gin.Context) {
|
||||
c.String(200, "User-agent: *\nDisallow: /")
|
||||
})
|
||||
|
||||
r.GET("/questions/:id/:title", func(c *gin.Context) {
|
||||
questionId := c.Param("id")
|
||||
questionTitle := c.Param("title")
|
||||
@ -100,14 +104,52 @@ func main() {
|
||||
|
||||
doc.Find("div.answer").Each(func(i int, s *goquery.Selection) {
|
||||
postLayout := s.Find("div.post-layout")
|
||||
voteCell := postLayout.Find("div.votecell")
|
||||
answerCell := postLayout.Find("div.answercell")
|
||||
answerBody := answerCell.Find("div.s-prose")
|
||||
answerBodyHTML, _ := answerBody.Html()
|
||||
|
||||
voteCount := voteCell.Find("div.js-vote-count").Text()
|
||||
|
||||
if s.HasClass("accepted-answer") {
|
||||
// add <div class="accepted-answer">Accepted Answer</div> to the top of the answer
|
||||
answerBodyHTML = `<div class="accepted-answer">Accepted Answer</div>` + answerBodyHTML
|
||||
// add <div class="answer-meta accepted">Accepted Answer</div> to the top of the answer
|
||||
answerBodyHTML = fmt.Sprintf(`<div class="answer-meta accepted">Accepted Answer - %s Upvotes</div>`, voteCount) + answerBodyHTML
|
||||
} else {
|
||||
// add <div class="answer-meta">%s Upvotes</div> to the top of the answer
|
||||
answerBodyHTML = fmt.Sprintf(`<div class="answer-meta">%s Upvotes</div>`, voteCount) + answerBodyHTML
|
||||
}
|
||||
|
||||
answerFooter := s.Find("div.mt24")
|
||||
|
||||
answerAuthorURL := ""
|
||||
answerAuthorName := ""
|
||||
answerTimestamp := ""
|
||||
|
||||
answerFooter.Find("div.post-signature").Each(func(i int, s *goquery.Selection) {
|
||||
answerAuthorDetails := s.Find("div.user-details")
|
||||
|
||||
if answerAuthorDetails.Length() == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
if answerAuthorDetails.Length() > 1 {
|
||||
if i == 0 {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
answerAuthor := answerAuthorDetails.Find("a").First()
|
||||
|
||||
answerAuthorURL = answerAuthor.AttrOr("href", "")
|
||||
answerAuthorName = answerAuthor.Text()
|
||||
answerTimestamp = s.Find("span.relativetime").Text()
|
||||
})
|
||||
|
||||
// append <div class="answer-author">Answered %s by %s</div> to the bottom of the answer
|
||||
answerBodyHTML += fmt.Sprintf(`<div class="answer-author-parent"><div class="answer-author">Answered at %s by <a href="https://stackoverflow.com/%s" target="_blank" rel="noopener noreferrer">%s</a></div></div>`, answerTimestamp, answerAuthorURL, answerAuthorName)
|
||||
|
||||
// get the timestamp and author
|
||||
|
||||
answers = append(answers, template.HTML(answerBodyHTML))
|
||||
})
|
||||
|
||||
|
@ -73,11 +73,30 @@ img {
|
||||
max-width: 100%;
|
||||
}
|
||||
|
||||
.accepted-answer {
|
||||
background-color: #8cffc0;
|
||||
.answer-meta {
|
||||
border-radius: 5px;
|
||||
padding: 1rem;
|
||||
color: black;
|
||||
margin-bottom: 1rem;
|
||||
/* width: fit-content; */
|
||||
background-color: rgb(82, 82, 98);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.answer-meta.accepted {
|
||||
background-color: #8cffc0 !important;
|
||||
color: black !important;
|
||||
}
|
||||
|
||||
.answer-author-parent {
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
}
|
||||
|
||||
.answer-author {
|
||||
width: fit-content;
|
||||
background-color: rgb(82, 82, 98);
|
||||
padding: .5rem;
|
||||
border-radius: 5px;
|
||||
text-align: right;
|
||||
font-size: .8rem;
|
||||
}
|
@ -15,7 +15,7 @@
|
||||
<h1>{{ .title }}</h1>
|
||||
<p class="timestamp">
|
||||
Asked {{ .timestamp }} by
|
||||
<a href="{{ .authorURL }}">{{ .author }}</a>.
|
||||
<a href="{{ .authorURL }}" target="_blank" rel="noopener noreferrer">{{ .author }}</a>.
|
||||
</p>
|
||||
</div>
|
||||
<div class="card-body">{{ .body }}</div>
|
||||
|
Loading…
x
Reference in New Issue
Block a user