2021-05-05 20:34:32 +00:00
package hmnurl
import (
"regexp"
"strconv"
"strings"
2021-05-06 04:04:58 +00:00
"git.handmade.network/hmn/hmn/src/oops"
2021-05-05 20:34:32 +00:00
)
2021-05-06 05:57:14 +00:00
var RegexHomepage = regexp . MustCompile ( "^/$" )
2021-05-05 20:34:32 +00:00
func BuildHomepage ( ) string {
return Url ( "/" , nil )
}
2021-05-06 05:57:14 +00:00
var RegexLogin = regexp . MustCompile ( "^/login$" )
2021-05-05 20:34:32 +00:00
func BuildLogin ( ) string {
return Url ( "/login" , nil )
}
2021-05-06 05:57:14 +00:00
var RegexLogout = regexp . MustCompile ( "^/logout$" )
2021-05-05 20:34:32 +00:00
func BuildLogout ( ) string {
return Url ( "/logout" , nil )
}
2021-05-06 05:57:14 +00:00
var RegexManifesto = regexp . MustCompile ( "^/manifesto$" )
2021-05-05 20:34:32 +00:00
func BuildManifesto ( ) string {
return Url ( "/manifesto" , nil )
}
2021-05-06 05:57:14 +00:00
var RegexAbout = regexp . MustCompile ( "^/about$" )
2021-05-05 20:34:32 +00:00
func BuildAbout ( ) string {
return Url ( "/about" , nil )
}
2021-05-06 05:57:14 +00:00
var RegexCodeOfConduct = regexp . MustCompile ( "^/code-of-conduct$" )
2021-05-05 20:34:32 +00:00
func BuildCodeOfConduct ( ) string {
return Url ( "/code-of-conduct" , nil )
}
2021-05-06 05:57:14 +00:00
var RegexCommunicationGuidelines = regexp . MustCompile ( "^/communication-guidelines$" )
2021-05-05 20:34:32 +00:00
func BuildCommunicationGuidelines ( ) string {
return Url ( "/communication-guidelines" , nil )
}
2021-05-06 05:57:14 +00:00
var RegexContactPage = regexp . MustCompile ( "^/contact$" )
2021-05-05 20:34:32 +00:00
func BuildContactPage ( ) string {
return Url ( "/contact" , nil )
}
2021-05-06 05:57:14 +00:00
var RegexMonthlyUpdatePolicy = regexp . MustCompile ( "^/monthly-update-policy$" )
2021-05-05 20:34:32 +00:00
func BuildMonthlyUpdatePolicy ( ) string {
return Url ( "/monthly-update-policy" , nil )
}
2021-05-06 05:57:14 +00:00
var RegexProjectSubmissionGuidelines = regexp . MustCompile ( "^/project-guidelines$" )
2021-05-05 20:34:32 +00:00
func BuildProjectSubmissionGuidelines ( ) string {
return Url ( "/project-guidelines" , nil )
}
2021-05-06 05:57:14 +00:00
var RegexFeed = regexp . MustCompile ( ` ^/feed(/(?P<page>.+)?)?$ ` )
2021-05-05 20:34:32 +00:00
func BuildFeed ( ) string {
return Url ( "/feed" , nil )
}
func BuildFeedWithPage ( page int ) string {
if page < 1 {
panic ( oops . New ( nil , "Invalid feed page (%d), must be >= 1" , page ) )
}
if page == 1 {
return BuildFeed ( )
}
return Url ( "/feed/" + strconv . Itoa ( page ) , nil )
}
2021-05-06 05:57:14 +00:00
var RegexForumThread = regexp . MustCompile ( ` ^/(?P<cats>forums(/[^\d]+?)*)/t/(?P<threadid>\d+)(/(?P<page>\d+))?$ ` )
2021-05-05 20:34:32 +00:00
func BuildForumThread ( projectSlug string , subforums [ ] string , threadId int , page int ) string {
if page < 1 {
panic ( oops . New ( nil , "Invalid forum thread page (%d), must be >= 1" , page ) )
}
var builder strings . Builder
builder . WriteString ( "/forums" )
for _ , subforum := range subforums {
subforum = strings . TrimSpace ( subforum )
if strings . Contains ( subforum , "/" ) {
panic ( oops . New ( nil , "Tried building forum thread url with / in subforum name" ) )
}
if len ( subforum ) == 0 {
panic ( oops . New ( nil , "Tried building forum thread url with blank subforum" ) )
}
builder . WriteRune ( '/' )
builder . WriteString ( subforum )
}
builder . WriteString ( "/t/" )
builder . WriteString ( strconv . Itoa ( threadId ) )
if page > 1 {
builder . WriteRune ( '/' )
builder . WriteString ( strconv . Itoa ( page ) )
}
return ProjectUrl ( builder . String ( ) , nil , projectSlug )
}
2021-05-06 05:57:14 +00:00
var RegexForumCategory = regexp . MustCompile ( ` ^/(?P<cats>forums(/[^\d]+?)*)(/(?P<page>\d+))?$ ` )
2021-05-05 20:34:32 +00:00
func BuildForumCategory ( projectSlug string , subforums [ ] string , page int ) string {
if page < 1 {
panic ( oops . New ( nil , "Invalid forum thread page (%d), must be >= 1" , page ) )
}
var builder strings . Builder
builder . WriteString ( "/forums" )
for _ , subforum := range subforums {
subforum = strings . TrimSpace ( subforum )
if strings . Contains ( subforum , "/" ) {
panic ( oops . New ( nil , "Tried building forum thread url with / in subforum name" ) )
}
if len ( subforum ) == 0 {
panic ( oops . New ( nil , "Tried building forum thread url with blank subforum" ) )
}
builder . WriteRune ( '/' )
builder . WriteString ( subforum )
}
if page > 1 {
builder . WriteRune ( '/' )
builder . WriteString ( strconv . Itoa ( page ) )
}
return ProjectUrl ( builder . String ( ) , nil , projectSlug )
}
2021-05-06 05:57:14 +00:00
var RegexForumPost = regexp . MustCompile ( ` ` ) // TODO(asaf): Complete this and test it
2021-05-05 20:34:32 +00:00
func BuildForumPost ( projectSlug string , subforums [ ] string , threadId int , postId int ) string {
var builder strings . Builder
builder . WriteString ( "/forums" )
for _ , subforum := range subforums {
subforum = strings . TrimSpace ( subforum )
if strings . Contains ( subforum , "/" ) {
panic ( oops . New ( nil , "Tried building forum thread url with / in subforum name" ) )
}
if len ( subforum ) == 0 {
panic ( oops . New ( nil , "Tried building forum thread url with blank subforum" ) )
}
builder . WriteRune ( '/' )
builder . WriteString ( subforum )
}
builder . WriteString ( "/t/" )
builder . WriteString ( strconv . Itoa ( threadId ) )
builder . WriteString ( "/p/" )
builder . WriteString ( strconv . Itoa ( postId ) )
return ProjectUrl ( builder . String ( ) , nil , projectSlug )
}
2021-05-06 05:57:14 +00:00
var RegexForumPostDelete = regexp . MustCompile ( ` ` ) // TODO
func BuildForumPostDelete ( projectSlug string , subforums [ ] string , threadId int , postId int ) string {
return BuildForumPost ( projectSlug , subforums , threadId , postId ) + "/delete"
}
var RegexForumPostEdit = regexp . MustCompile ( ` ` ) // TODO
func BuildForumPostEdit ( projectSlug string , subforums [ ] string , threadId int , postId int ) string {
return BuildForumPost ( projectSlug , subforums , threadId , postId ) + "/edit"
}
var RegexForumPostReply = regexp . MustCompile ( ` ` ) // TODO(asaf): Ha ha! I, Ben, have played a trick on you, and forced you to do this regex as well!
// TODO: It's kinda weird that we have "replies" to a specific post. That's not how the data works. I guess this just affects what you see as the "post you're replying to" on the post composer page?
func BuildForumPostReply ( projectSlug string , subforums [ ] string , threadId int , postId int ) string {
return BuildForumPost ( projectSlug , subforums , threadId , postId ) + "/reply"
}
var RegexForumPostQuote = regexp . MustCompile ( ` ` ) // TODO
func BuildForumPostQuote ( projectSlug string , subforums [ ] string , threadId int , postId int ) string {
return BuildForumPost ( projectSlug , subforums , threadId , postId ) + "/quote"
}
var RegexProjectCSS = regexp . MustCompile ( "^/assets/project.css$" )
2021-05-05 20:34:32 +00:00
func BuildProjectCSS ( color string ) string {
2021-05-06 04:04:58 +00:00
return Url ( "/assets/project.css" , [ ] Q { { "color" , color } } )
2021-05-05 20:34:32 +00:00
}
2021-05-06 05:57:14 +00:00
var RegexPublic = regexp . MustCompile ( "^/public/.+$" )
2021-05-05 20:34:32 +00:00
func BuildPublic ( filepath string ) string {
filepath = strings . Trim ( filepath , "/" )
if len ( strings . TrimSpace ( filepath ) ) == 0 {
panic ( oops . New ( nil , "Attempted to build a /public url with no path" ) )
}
var builder strings . Builder
builder . WriteString ( "/public" )
pathParts := strings . Split ( filepath , "/" )
for _ , part := range pathParts {
part = strings . TrimSpace ( part )
if len ( part ) == 0 {
panic ( oops . New ( nil , "Attempted to build a /public url with blank path segments: %s" , filepath ) )
}
builder . WriteRune ( '/' )
builder . WriteString ( part )
}
return Url ( builder . String ( ) , nil )
}
2021-05-06 05:57:14 +00:00
var RegexCatchAll = regexp . MustCompile ( "" )