all repos

clerk @ 8540205899ad32eb84a181f05f2ae0eab57e0a18

missing tooling for ledger/hledger

clerk/journal/printer/directives.go (view raw)

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
journal/printer: write IgnoredDirective up, 1 month ago
1
package printer
2
3
import (
4
	"strconv"
5
	"strings"
6
7
	"olexsmir.xyz/clerk/journal/ast"
8
)
9
10
func (p *printer) writeIncludeDirective(i *ast.IncludeDirective) {
11
	p.buf.WriteString("include ")
12
	p.buf.WriteString(quoteString(i.Path))
13
	p.writeInlineComment(i.Comment)
14
}
15
16
func (p *printer) writeAccountDirective(a *ast.AccountDirective) {
17
	p.buf.WriteString("account ")
18
	p.buf.WriteString(a.Account.Name)
19
	p.writeInlineComment(a.Comment)
20
}
21
22
func (p *printer) writeCommodityDirective(c *ast.CommodityDirective) {
23
	p.buf.WriteString("commodity ")
24
	p.buf.WriteString(c.Commodity)
25
	p.writeInlineComment(c.Comment)
26
}
27
28
func (p *printer) writeAliasDirective(a *ast.AliasDirective) {
29
	p.buf.WriteString("alias ")
30
	p.buf.WriteString(a.From)
31
	p.buf.WriteString(" = ")
32
	p.buf.WriteString(a.To)
33
	p.writeInlineComment(a.Comment)
34
}
35
36
func (p *printer) writePayeeDirective(pd *ast.PayeeDirective) {
37
	p.buf.WriteString("payee ")
38
	p.buf.WriteString(quoteString(pd.Name))
39
	p.writeInlineComment(pd.Comment)
40
}
41
42
func (p *printer) writeTagDirective(t *ast.TagDirective) {
43
	p.buf.WriteString("tag ")
44
	p.buf.WriteString(quoteString(t.Name))
45
	p.writeInlineComment(t.Comment)
46
}
47
48
func (p *printer) writeYearDirective(y *ast.YearDirective) {
49
	p.buf.WriteString("year ")
50
	p.writeInt(y.Year, 4)
51
	p.writeInlineComment(y.Comment)
52
}
53
54
func (p *printer) writeDecimalMarkDirective(d *ast.DecimalMarkDirective) {
55
	p.buf.WriteString("decimal-mark ")
56
	p.buf.WriteByte(d.Mark)
57
	p.writeInlineComment(d.Comment)
58
}
59
60
func (p *printer) writeDefaultCommodityDirective(d *ast.DefaultCommodityDirective) {
61
	p.buf.WriteString("D ")
62
	p.writeAmount(&d.Amount, p.cfg.CommodityPos)
63
	p.writeInlineComment(d.Comment)
64
}
65
66
func (p *printer) writeConversionDirective(c *ast.ConversionDirective) {
67
	p.buf.WriteString("C ")
68
	p.writeAmount(&c.From, p.cfg.CommodityPos)
69
	p.buf.WriteString(" = ")
70
	p.writeAmount(&c.To, p.cfg.CommodityPos)
71
	p.writeInlineComment(c.Comment)
72
}
73
74
func (p *printer) writeMarketPriceDirective(m *ast.MarketPriceDirective) {
75
	p.buf.WriteString("P ")
76
	p.writeDate(m.DateTime.Date)
77
	p.writeTime(m.DateTime.Time)
78
	p.buf.WriteByte(' ')
79
	p.buf.WriteString(m.Commodity)
80
	p.buf.WriteByte(' ')
81
	p.writeAmount(&m.Amount, p.cfg.CommodityPos)
82
	p.writeInlineComment(m.Comment)
83
}
84
85
func (p *printer) writeCommentBlockDirective(cb *ast.CommentBlockDirective) {
86
	p.buf.WriteString("comment")
87
	if cb.Header != "" {
88
		p.buf.WriteByte(' ')
89
		p.buf.WriteString(cb.Header)
90
	}
91
	p.buf.WriteByte('\n')
92
	if cb.Content != "" {
93
		p.buf.WriteString(cb.Content)
94
		if !strings.HasSuffix(cb.Content, "\n") {
95
			p.buf.WriteByte('\n')
96
		}
97
	}
98
	p.buf.WriteString("end")
99
	if cb.Header != "" {
100
		p.buf.WriteByte(' ')
101
		p.buf.WriteString(cb.Header)
102
	}
103
}
104
105
func (p *printer) writeApplyDirective(a *ast.ApplyDirective) {
106
	p.buf.WriteString("apply ")
107
	p.buf.WriteString(a.Expr)
108
	p.writeInlineComment(a.Comment)
109
}
110
111
func (p *printer) writeEndDirective(e *ast.EndDirective) {
112
	p.buf.WriteString("end")
113
	if e.Expr != "" {
114
		p.buf.WriteByte(' ')
115
		p.buf.WriteString(e.Expr)
116
	}
117
	p.writeInlineComment(e.Comment)
118
}
119
120
func (p *printer) writeIgnoredDirective(e *ast.IgnoredDirective) {
121
	p.buf.WriteString("N ")
122
	p.buf.WriteString(e.Text)
123
	p.writeInlineComment(e.Comment)
124
}
125
126
func quoteString(s string) string {
127
	needsQuote := false
128
	for _, c := range s {
129
		if c == ' ' || c == '\t' || c == '"' || c == ';' || c == '#' {
130
			needsQuote = true
131
			break
132
		}
133
	}
134
	if !needsQuote {
135
		return s
136
	}
137
	return strconv.Quote(s)
138
}