2020-10-30 13:25:48 +00:00
// This is not done yet
package markup
import (
2020-12-17 12:59:59 +00:00
"fmt"
2020-10-30 13:25:48 +00:00
"html"
2020-12-17 12:59:59 +00:00
"regexp"
2020-10-30 13:25:48 +00:00
"strings"
2020-12-17 12:59:59 +00:00
"github.com/bouncepaw/mycorrhiza/util"
2020-10-30 13:25:48 +00:00
)
// A Mycomarkup-formatted document
type MycoDoc struct {
// data
hyphaName string
contents string
2020-12-17 12:59:59 +00:00
// indicators
parsedAlready bool
// results
ast [ ] Line
html string
firstImageURL string
description string
2020-10-30 13:25:48 +00:00
}
// Constructor
func Doc ( hyphaName , contents string ) * MycoDoc {
2020-12-17 12:59:59 +00:00
md := & MycoDoc {
2020-10-30 13:25:48 +00:00
hyphaName : hyphaName ,
contents : contents ,
}
2020-12-17 12:59:59 +00:00
return md
}
func ( md * MycoDoc ) Lex ( recursionLevel int ) * MycoDoc {
if ! md . parsedAlready {
md . ast = md . lex ( )
}
md . parsedAlready = true
return md
2020-10-30 13:25:48 +00:00
}
// AsHtml returns an html representation of the document
2020-12-15 18:59:36 +00:00
func ( md * MycoDoc ) AsHTML ( ) string {
2020-12-17 12:59:59 +00:00
md . html = Parse ( md . Lex ( 0 ) . ast , 0 , 0 , 0 )
return md . html
2020-12-15 18:59:36 +00:00
}
2020-12-17 12:59:59 +00:00
// Used to clear opengraph description from html tags. This method is usually bad because of dangers of malformed HTML, but I'm going to use it only for Mycorrhiza-generated HTML, so it's okay. The question mark is required; without it the whole string is eaten away.
var htmlTagRe = regexp . MustCompile ( ` <.*?> ` )
2020-12-15 18:59:36 +00:00
// OpenGraphHTML returns an html representation of og: meta tags.
func ( md * MycoDoc ) OpenGraphHTML ( ) string {
2020-12-17 12:59:59 +00:00
md . ogFillVars ( )
return strings . Join ( [ ] string {
ogTag ( "title" , md . hyphaName ) ,
ogTag ( "type" , "article" ) ,
ogTag ( "image" , md . firstImageURL ) ,
ogTag ( "url" , util . URL + "/page/" + md . hyphaName ) ,
ogTag ( "determiner" , "" ) ,
ogTag ( "description" , htmlTagRe . ReplaceAllString ( md . description , "" ) ) ,
} , "\n" )
}
func ( md * MycoDoc ) ogFillVars ( ) * MycoDoc {
foundDesc := false
md . firstImageURL = HyphaImageForOG ( md . hyphaName )
for _ , line := range md . ast {
switch v := line . contents . ( type ) {
case string :
if ! foundDesc {
md . description = v
foundDesc = true
}
case Img :
if len ( v . entries ) > 0 {
md . firstImageURL = v . entries [ 0 ] . path . String ( )
}
}
}
return md
}
func ogTag ( property , content string ) string {
return fmt . Sprintf ( ` <meta property="og:%s" content="%s"/> ` , property , content )
2020-10-30 13:25:48 +00:00
}
2020-12-15 18:59:36 +00:00
/* The rest of this file is currently unused. TODO: use it I guess */
2020-10-30 13:25:48 +00:00
type BlockType int
const (
BlockH1 = iota
BlockH2
BlockH3
BlockH4
BlockH5
BlockH6
BlockRocket
BlockPre
BlockQuote
BlockPara
)
type CrawlWhere int
const (
inSomewhere = iota
inPre
inEnd
)
func crawl ( name , content string ) [ ] string {
stateStack := [ ] CrawlWhere { inSomewhere }
startsWith := func ( token string ) bool {
return strings . HasPrefix ( content , token )
}
pop := func ( ) {
stateStack = stateStack [ : len ( stateStack ) - 1 ]
}
push := func ( s CrawlWhere ) {
stateStack = append ( stateStack , s )
}
readln := func ( c string ) ( string , string ) {
parts := strings . SplitN ( c , "\n" , 1 )
return parts [ 0 ] , parts [ 1 ]
}
preAcc := ""
line := ""
for {
switch stateStack [ 0 ] {
case inSomewhere :
switch {
case startsWith ( "```" ) :
push ( inPre )
_ , content = readln ( content )
default :
}
case inPre :
switch {
case startsWith ( "```" ) :
pop ( )
_ , content = readln ( content )
default :
line , content = readln ( content )
preAcc += html . EscapeString ( line )
}
}
}
return [ ] string { }
}