summaryrefslogtreecommitdiffstats
path: root/modules/markup/markdown/ast.go
blob: d735ff5ebd847cda4e7a1a13d049691b32ce0c76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package markdown

import (
	"strconv"

	"github.com/yuin/goldmark/ast"
)

// Details is a block that contains Summary and details
type Details struct {
	ast.BaseBlock
}

// Dump implements Node.Dump .
func (n *Details) Dump(source []byte, level int) {
	ast.DumpHelper(n, source, level, nil, nil)
}

// KindDetails is the NodeKind for Details
var KindDetails = ast.NewNodeKind("Details")

// Kind implements Node.Kind.
func (n *Details) Kind() ast.NodeKind {
	return KindDetails
}

// NewDetails returns a new Paragraph node.
func NewDetails() *Details {
	return &Details{
		BaseBlock: ast.BaseBlock{},
	}
}

// IsDetails returns true if the given node implements the Details interface,
// otherwise false.
func IsDetails(node ast.Node) bool {
	_, ok := node.(*Details)
	return ok
}

// Summary is a block that contains the summary of details block
type Summary struct {
	ast.BaseBlock
}

// Dump implements Node.Dump .
func (n *Summary) Dump(source []byte, level int) {
	ast.DumpHelper(n, source, level, nil, nil)
}

// KindSummary is the NodeKind for Summary
var KindSummary = ast.NewNodeKind("Summary")

// Kind implements Node.Kind.
func (n *Summary) Kind() ast.NodeKind {
	return KindSummary
}

// NewSummary returns a new Summary node.
func NewSummary() *Summary {
	return &Summary{
		BaseBlock: ast.BaseBlock{},
	}
}

// IsSummary returns true if the given node implements the Summary interface,
// otherwise false.
func IsSummary(node ast.Node) bool {
	_, ok := node.(*Summary)
	return ok
}

// TaskCheckBoxListItem is a block that repressents a list item of a markdown block with a checkbox
type TaskCheckBoxListItem struct {
	*ast.ListItem
	IsChecked bool
}

// KindTaskCheckBoxListItem is the NodeKind for TaskCheckBoxListItem
var KindTaskCheckBoxListItem = ast.NewNodeKind("TaskCheckBoxListItem")

// Dump implements Node.Dump .
func (n *TaskCheckBoxListItem) Dump(source []byte, level int) {
	m := map[string]string{}
	m["IsChecked"] = strconv.FormatBool(n.IsChecked)
	ast.DumpHelper(n, source, level, m, nil)
}

// Kind implements Node.Kind.
func (n *TaskCheckBoxListItem) Kind() ast.NodeKind {
	return KindTaskCheckBoxListItem
}

// NewTaskCheckBoxListItem returns a new TaskCheckBoxListItem node.
func NewTaskCheckBoxListItem(listItem *ast.ListItem) *TaskCheckBoxListItem {
	return &TaskCheckBoxListItem{
		ListItem: listItem,
	}
}

// IsTaskCheckBoxListItem returns true if the given node implements the TaskCheckBoxListItem interface,
// otherwise false.
func IsTaskCheckBoxListItem(node ast.Node) bool {
	_, ok := node.(*TaskCheckBoxListItem)
	return ok
}

// Icon is an inline for a fomantic icon
type Icon struct {
	ast.BaseInline
	Name []byte
}

// Dump implements Node.Dump .
func (n *Icon) Dump(source []byte, level int) {
	m := map[string]string{}
	m["Name"] = string(n.Name)
	ast.DumpHelper(n, source, level, m, nil)
}

// KindIcon is the NodeKind for Icon
var KindIcon = ast.NewNodeKind("Icon")

// Kind implements Node.Kind.
func (n *Icon) Kind() ast.NodeKind {
	return KindIcon
}

// NewIcon returns a new Paragraph node.
func NewIcon(name string) *Icon {
	return &Icon{
		BaseInline: ast.BaseInline{},
		Name:       []byte(name),
	}
}

// IsIcon returns true if the given node implements the Icon interface,
// otherwise false.
func IsIcon(node ast.Node) bool {
	_, ok := node.(*Icon)
	return ok
}
anging from 0 to 255 * * @param theRed the red integer value * @param theGreen the green integer value * @param theBlue the blue integer value */ public PDFColor(int theRed, int theGreen, int theBlue) { this(((double)theRed) / 255d, ((double)theGreen) / 255d, ((double)theBlue) / 255d); } /** * Create a PDF color with CMYK values. * * @param theCyan the cyan value * @param theMagenta the magenta value * @param theYellow the yellow value * @param theBlack the black value */ public PDFColor(double theCyan, double theMagenta, double theYellow, double theBlack) { // super(theNumber);//? this.colorSpace = new PDFColorSpace(PDFColorSpace.DEVICE_CMYK); this.cyan = theCyan; this.magenta = theMagenta; this.yellow = theYellow; this.black = theBlack; } /** * Return a vector representation of the color * in the appropriate colorspace. * * @return a list containing the Double values of the color */ public List getVector() { List theColorVector = new ArrayList(); if (this.colorSpace.getColorSpace() == PDFColorSpace.DEVICE_RGB) { // RGB theColorVector.add(new Double(this.red)); theColorVector.add(new Double(this.green)); theColorVector.add(new Double(this.blue)); } else if (this.colorSpace.getColorSpace() == PDFColorSpace.DEVICE_CMYK) { // CMYK theColorVector.add(new Double(this.cyan)); theColorVector.add(new Double(this.magenta)); theColorVector.add(new Double(this.yellow)); theColorVector.add(new Double(this.black)); } else { // GRAY theColorVector.add(new Double(this.black)); } return (theColorVector); } /** * Get the red component. * * @return the red double value */ public double red() { return (this.red); } /** * Get the green component. * * @return the green double value */ public double green() { return (this.green); } /** * Get the blue component. * * @return the blue double value */ public double blue() { return (this.blue); } /** * Get the red integer component. * * @return the red integer value */ public int red255() { return (int)(this.red * 255d); } /** * Get the green integer component. * * @return the green integer value */ public int green255() { return (int)(this.green * 255d); } /** * Get the blue integer component. * * @return the blue integer value */ public int blue255() { return (int)(this.blue * 255d); } /** * Get the cyan component. * * @return the cyan double value */ public double cyan() { return (this.cyan); } /** * Get the magenta component. * * @return the magenta double value */ public double magenta() { return (this.magenta); } /** * Get the yellow component. * * @return the yellow double value */ public double yellow() { return (this.yellow); } /** * Get the black component. * * @return the black double value */ public double black() { return (this.black); } /** * Set the color space for this color. * If the new color space is different the values are converted * to the new color space. * * @param theColorSpace the new color space */ public void setColorSpace(int theColorSpace) { int theOldColorSpace = this.colorSpace.getColorSpace(); if (theOldColorSpace != theColorSpace) { if (theOldColorSpace == PDFColorSpace.DEVICE_RGB) { if (theColorSpace == PDFColorSpace.DEVICE_CMYK) { this.convertRGBtoCMYK(); } else { // convert to Gray? this.convertRGBtoGRAY(); } } else if (theOldColorSpace == PDFColorSpace.DEVICE_CMYK) { if (theColorSpace == PDFColorSpace.DEVICE_RGB) { this.convertCMYKtoRGB(); } else { // convert to Gray? this.convertCMYKtoGRAY(); } } else { // used to be Gray if (theColorSpace == PDFColorSpace.DEVICE_RGB) { this.convertGRAYtoRGB(); } else { // convert to CMYK? this.convertGRAYtoCMYK(); } } this.colorSpace.setColorSpace(theColorSpace); } } /** * Get the PDF output string for this color. * This returns the string to be inserted into PDF for setting * the current color. * * @param fillNotStroke whether to return fill or stroke command * @return the PDF string for setting the fill/stroke color */ public String getColorSpaceOut(boolean fillNotStroke) { StringBuffer p = new StringBuffer(""); double tempDouble; if (this.colorSpace.getColorSpace() == PDFColorSpace.DEVICE_RGB) { // colorspace is RGB // according to pdfspec 12.1 p.399 // if the colors are the same then just use the g or G operator boolean same = false; if (this.red == this.green && this.red == this.blue) { same = true; } // output RGB if (fillNotStroke) { // fill if (same) { p.append(PDFNumber.doubleOut(this.red) + " g\n"); } else { p.append(PDFNumber.doubleOut(this.red) + " " + PDFNumber.doubleOut(this.green) + " " + PDFNumber.doubleOut(this.blue) + " rg\n"); } } else { // stroke/border if (same) { p.append(PDFNumber.doubleOut(this.red) + " G\n"); } else { p.append(PDFNumber.doubleOut(this.red) + " " + PDFNumber.doubleOut(this.green) + " " + PDFNumber.doubleOut(this.blue) + " RG\n"); } } } else if (this.colorSpace.getColorSpace() == PDFColorSpace.DEVICE_CMYK) { // colorspace is CMYK if (fillNotStroke) { // fill p.append(PDFNumber.doubleOut(this.cyan) + " " + PDFNumber.doubleOut(this.magenta) + " " + PDFNumber.doubleOut(this.yellow) + " " + PDFNumber.doubleOut(this.black) + " k\n"); } else { // stroke p.append(PDFNumber.doubleOut(this.cyan) + " " + PDFNumber.doubleOut(this.magenta) + " " + PDFNumber.doubleOut(this.yellow) + " " + PDFNumber.doubleOut(this.black) + " K\n"); } } else { // means we're in DeviceGray or Unknown. // assume we're in DeviceGray, because otherwise we're screwed. if (fillNotStroke) { p.append(PDFNumber.doubleOut(this.black) + " g\n"); } else { p.append(PDFNumber.doubleOut(this.black) + " G\n"); } } return (p.toString()); } /** * Convert the color from CMYK to RGB. */ protected void convertCMYKtoRGB() { // convert CMYK to RGB this.red = 1.0 - this.cyan; this.green = 1.0 - this.green; this.blue = 1.0 - this.yellow; this.red = (this.black / this.blackFactor) + this.red; this.green = (this.black / this.blackFactor) + this.green; this.blue = (this.black / this.blackFactor) + this.blue; } /** * Convert the color from RGB to CMYK. */ protected void convertRGBtoCMYK() { // convert RGB to CMYK this.cyan = 1.0 - this.red; this.magenta = 1.0 - this.green; this.yellow = 1.0 - this.blue; this.black = 0.0; /* * If you want to calculate black, uncomment this * //pick the lowest color * tempDouble = this.red; * * if (this.green < tempDouble) * tempDouble = this.green; * * if (this.blue < tempDouble) * tempDouble = this.blue; * * this.black = tempDouble / this.blackFactor; */ } /** * Convert the color from Gray to RGB. */ protected void convertGRAYtoRGB() { this.red = 1.0 - this.black; this.green = 1.0 - this.black; this.blue = 1.0 - this.black; } /** * Convert the color from Gray to CMYK. */ protected void convertGRAYtoCMYK() { this.cyan = this.black; this.magenta = this.black; this.yellow = this.black; // this.black=0.0;//? } /** * Convert the color from CMYK to Gray. */ protected void convertCMYKtoGRAY() { double tempDouble = 0.0; // pick the lowest color tempDouble = this.cyan; if (this.magenta < tempDouble) { tempDouble = this.magenta; } if (this.yellow < tempDouble) { tempDouble = this.yellow; } this.black = (tempDouble / this.blackFactor); } /** * Convert the color from RGB to Gray. */ protected void convertRGBtoGRAY() { double tempDouble = 0.0; // pick the lowest color tempDouble = this.red; if (this.green < tempDouble) { tempDouble = this.green; } if (this.blue < tempDouble) { tempDouble = this.blue; } this.black = 1.0 - (tempDouble / this.blackFactor); } /** * Create pdf. * Not used for this object. * * @return the bytes for the pdf */ public byte[] toPDF() { return (new byte[0]); } /** * Check for equality of color with another object. * * @param obj the object to compare * @return true if colors are equal */ public boolean equals(Object obj) { if (!(obj instanceof PDFColor)) { return false; } PDFColor color = (PDFColor)obj; if (color.red == this.red && color.green == this.green && color.blue == this.blue) { return true; } return false; } }