This repository has been archived by the owner on Aug 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
interface.go
84 lines (67 loc) · 2.64 KB
/
interface.go
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
// Package mycocontext provides a wrapper over context.Context and some operations on the wrapper.
package mycocontext
import (
"bytes"
"context"
"github.com/bouncepaw/mycomarkup/v5/options"
)
// Context is the wrapper around context.Context providing type-level safety on presence of several values.
type Context interface {
context.Context
// HyphaName returns the name of the processed hypha.
HyphaName() string
// Input returns the buffer which contains all characters of the hypha text.
Input() *bytes.Buffer
// RecursionLevel returns current recursive transclusion level.
RecursionLevel() uint
// WithIncrementedRecursionLevel returns a copy of the context but with the recursion level incremented.
//
// lvl1 := ctx.RecursionLevel()
// lvl2 := ctx.WithIncrementedRecursionLevel().RecursionLevel()
// lvl2 - lvl1 == 1
WithIncrementedRecursionLevel() Context
// WebSiteURL returns the URL of the wiki, including the protocol (http or https). It is used for generating OpenGraph meta tags.
WebSiteURL() string
// TransclusionSupported is true if Mycomarkup is invoked as a stand-alone program, false if used as a library.
TransclusionSupported() bool
}
func Options(ctx Context) options.Options {
return ctx.Value(keyOptions).(options.Options)
}
// CancelFunc is a function you call to cancel the context. Why would you, though?
type CancelFunc context.CancelFunc
// ContextFromStringInput returns the context for the given input.
func ContextFromStringInput(input string, opts options.Options) (Context, CancelFunc) {
opts.TransclusionSupported = true
ctx, cancel := context.WithCancel(
context.WithValue(
context.WithValue(
context.WithValue(
context.Background(),
keyOptions,
opts),
keyInputBuffer,
bytes.NewBufferString(input),
),
keyRecursionLevel,
uint(0),
))
return &mycoContext{ctx}, CancelFunc(cancel)
}
// WithBuffer returns a copy of the given context but with a different input buffer.
func WithBuffer(ctx Context, buf *bytes.Buffer) Context {
return &mycoContext{context.WithValue(ctx, keyInputBuffer, buf)}
}
func WithOptions(ctx Context, opts options.Options) Context {
return &mycoContext{context.WithValue(ctx, keyOptions, opts)}
}
// TODO: get rid of these three below
func HyphaExists(ctx Context, hyphaName string) bool {
return ctx.Value(keyOptions).(options.Options).HyphaExists(hyphaName)
}
func IterateHyphaNamesWith(ctx Context, f func(string)) {
ctx.Value(keyOptions).(options.Options).IterateHyphaNamesWith(f)
}
func HyphaHTMLData(ctx Context, name string) (rawText, binaryHtml string, err error) {
return ctx.Value(keyOptions).(options.Options).HyphaHTMLData(name)
}