Skip to content

Commit

Permalink
Added a new details config + using bit flags
Browse files Browse the repository at this point in the history
Signed-off-by: Sandy <sandy@sandyuraz.com>
  • Loading branch information
thecsw committed Oct 23, 2022
1 parent 22065aa commit 4996ac2
Showing 6 changed files with 147 additions and 116 deletions.
6 changes: 3 additions & 3 deletions html/content.go
Original file line number Diff line number Diff line change
@@ -60,13 +60,13 @@ func paragraph(content *internals.Content) string {
</div>`,
// div class
func() string {
if content.IsQuote {
if content.IsQuote() {
return " quote"
}
if content.IsCentered {
if content.IsCentered() {
return " center"
}
if content.IsDropCap {
if content.IsDropCap() {
return " dropcap"
}
return ""
44 changes: 12 additions & 32 deletions internals/content.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,12 @@
package internals

// TypeContent is the type of content, used for enums
type TypeContent uint8

const (
// TypeHeading is the type of heading
TypeHeading TypeContent = iota
// TypeParagraph is the type of paragraph, which is just text
TypeParagraph
// TypeList is the type of unordered list
TypeList
// TypeListNumbered is the type of numbered list
TypeListNumbered
// TypeLink is the type of link
TypeLink
// TypeSourceCode is the type of source code block
TypeSourceCode
// TypeRawHTML is the type of raw HTML block
TypeRawHTML
// TypeHorizontalLine is the type of horizontal line
TypeHorizontalLine
// TypeAttentionText is the type of attention text block
TypeAttentionText
// TypeTable is the type of a table
TypeTable
)

// Content is a piece of content of a page
type Content struct {
// Type is the type of content
Type TypeContent

// Options tells us about the options enabled on the type
Options Bits
// HeadingLevel is the heading level of the content (1 being the title, starts at 2)
HeadingLevel int
// HeadingChild tells us if the current heading is a child of some previous heading
@@ -61,19 +37,15 @@ type Content struct {
AttentionTitle string
// AttentionText is the attention text
AttentionText string
// IsQuote defines if this part is a quote or not
IsQuote bool
// IsCentered define if this part should be centered
IsCentered bool
// IsDropCap defines whether to enlarge the first letter
IsDropCap bool
// Table is the table of items
Table [][]string
// TableHeaders tell us whether the table has headers
// (use the first row as headers instead of data)
TableHeaders bool
// Caption is the current caption
Caption string
// Summary is the current summary
Summary string
}

// IsHeading tells us if the content is a heading
@@ -105,3 +77,11 @@ func (c Content) IsAttentionBlock() bool { return c.Type == TypeAttentionText }

// IsTable tells us if the content block is a table
func (c Content) IsTable() bool { return c.Type == TypeTable }

func (c Content) IsCentered() bool { return HasFlag(&c.Options, InCenterFlag) }

func (c Content) IsDetails() bool { return HasFlag(&c.Options, InDetailsFlag) }

func (c Content) IsDropCap() bool { return HasFlag(&c.Options, InDropCapFlag) }

func (c Content) IsQuote() bool { return HasFlag(&c.Options, InQuoteFlag) }
58 changes: 58 additions & 0 deletions internals/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package internals

// TypeContent is the type of content, used for enums
type TypeContent uint8

const (
// TypeHeading is the type of heading
TypeHeading TypeContent = iota
// TypeParagraph is the type of paragraph, which is just text
TypeParagraph
// TypeList is the type of unordered list
TypeList
// TypeListNumbered is the type of numbered list
TypeListNumbered
// TypeLink is the type of link
TypeLink
// TypeSourceCode is the type of source code block
TypeSourceCode
// TypeRawHTML is the type of raw HTML block
TypeRawHTML
// TypeHorizontalLine is the type of horizontal line
TypeHorizontalLine
// TypeAttentionText is the type of attention text block
TypeAttentionText
// TypeTable is the type of a table
TypeTable
// TypeDetails is the type for html details
TypeDetails
)

type Bits uint16

const (
InListFlag Bits = 1 << iota
InTableFlag
InTableHasHeadersFlag
InSourceCodeFlag
InRawHTMLFlag
InQuoteFlag
InCenterFlag
InDetailsFlag
InDropCapFlag
)

var (
LatchFlags = func(v *Bits) (
func(f Bits), func(f Bits), func(f Bits), func(f Bits) bool) {
return func(f Bits) { AddFlag(v, f) },
func(f Bits) { RemoveFlag(v, f) },
func(f Bits) { FlipFlag(v, f) },
func(f Bits) bool { return HasFlag(v, f) }
}

AddFlag = func(v *Bits, f Bits) { *v |= f }
RemoveFlag = func(v *Bits, f Bits) { *v &^= f }
FlipFlag = func(v *Bits, f Bits) { *v ^= f }
HasFlag = func(v *Bits, f Bits) bool { return *v&f != 0 }
)
16 changes: 9 additions & 7 deletions orgmode/content.go
Original file line number Diff line number Diff line change
@@ -73,14 +73,16 @@ func isLink(line string) *internals.Content {
}
}

func formParagraph(text string, inQuote bool, inCenter bool, inDropCap bool) *internals.Content {
return &internals.Content{
Type: internals.TypeParagraph,
Paragraph: strings.TrimSpace(text),
IsCentered: inCenter,
IsQuote: inQuote,
IsDropCap: inDropCap,
func formParagraph(text, extra string, options internals.Bits) *internals.Content {
val := &internals.Content{
Type: internals.TypeParagraph,
Paragraph: strings.TrimSpace(text),
Options: options,
}
if internals.HasFlag(&options, internals.InDetailsFlag) {
val.Summary = extra
}
return val
}

func isList(line string) bool {
30 changes: 16 additions & 14 deletions orgmode/defs.go
Original file line number Diff line number Diff line change
@@ -7,20 +7,22 @@ import (
)

const (
CommentPrefix = "# "
OptionPrefix = "#+"
OptionDropCap = "drop_cap"
OptionBeginSource = "begin_src"
OptionEndSource = "end_src"
OptionBeginExport = "begin_export"
OptionEndExport = "end_export"
OptionBeginQuote = "begin_quote"
OptionEndQuote = "end_quote"
OptionBeginCenter = "begin_center"
OptionEndCenter = "end_center"
OptionCaption = "caption"
OptionDate = "date"
HorizontalLine = "---"
CommentPrefix = "# "
OptionPrefix = "#+"
OptionDropCap = "drop_cap"
OptionBeginSource = "begin_src"
OptionEndSource = "end_src"
OptionBeginExport = "begin_export"
OptionEndExport = "end_export"
OptionBeginQuote = "begin_quote"
OptionEndQuote = "end_quote"
OptionBeginCenter = "begin_center"
OptionEndCenter = "end_center"
OptionBeginDetails = "begin_details"
OptionEndDetails = "end_details"
OptionCaption = "caption"
OptionDate = "date"
HorizontalLine = "---"

SectionLevelOne = "* "
SectionLevelTwo = "** "
Loading

0 comments on commit 4996ac2

Please sign in to comment.