document.templ

  1package document
  2
  3templ DocumentComponent(d DocumentContent) {
  4	<style>
  5		.document rtw-waypoint {
  6		    font-weight: 700;
  7		}
  8		.document h1 {
  9		    font-size: 1.875rem;
 10			font-weight: 800;
 11		}
 12		.document {
 13		    padding-bottom: 10rem;
 14		}
 15	</style>
 16	<div class="document">
 17		@Fragment(d.Main)
 18	</div>
 19}
 20
 21templ Fragment(nodes []Node) {
 22	for _, node := range nodes {
 23		switch node.Type {
 24			case Heading:
 25				@HeadingComponent(NodeToAttrs[HeadingAttrs](node), node.Children...)
 26			case Paragraph:
 27				if len(node.Children) > 0 {
 28					@ParagraphComponent(node.Children...)
 29				}
 30			case OrderedList:
 31				@OrderedListComponent(NodeToAttrs[OrderedListAttrs](node), node.Children...)
 32			case BulletList:
 33				@BulletListComponent(node.Children...)
 34			default:
 35				<span class="error">unknown node type { node.Type }</span>
 36		}
 37	}
 38}
 39
 40templ HeadingComponent(attrs HeadingAttrs, children ...Node) {
 41	for _, node := range children {
 42		if node.Type != "text" {
 43			<span class="error">unknown node type: { node.Type }</span>
 44		} else {
 45			switch attrs.Level {
 46				case 1:
 47					<h1>
 48						@InlineTextComponent(NodeToTextContent(node))
 49					</h1>
 50				case 2:
 51					<h2>
 52						@InlineTextComponent(NodeToTextContent(node))
 53					</h2>
 54				case 3:
 55					<h3>
 56						@InlineTextComponent(NodeToTextContent(node))
 57					</h3>
 58				case 4:
 59					<h4>
 60						@InlineTextComponent(NodeToTextContent(node))
 61					</h4>
 62				case 5:
 63					<h5>
 64						@InlineTextComponent(NodeToTextContent(node))
 65					</h5>
 66				default:
 67					<h6>
 68						@InlineTextComponent(NodeToTextContent(node))
 69					</h6>
 70			}
 71		}
 72	}
 73}
 74
 75templ InlineTextComponent(segments []TextContent) {
 76	for _, seg := range segments {
 77		if seg.Type == 0 {
 78			{ seg.Content }
 79		} else {
 80			<span class={ seg.Type.Classes() }>{ seg.Content }</span>
 81		}
 82	}
 83}
 84
 85templ WaypointComponent(wc WaypointContent) {
 86	<rtw-waypoint lnglat={ wc["point"] } country={ wc["country"] }>{ wc["label"] }</rtw-waypoint>
 87}
 88
 89templ ParagraphComponent(children ...Node) {
 90	<p>
 91		for _, child := range children {
 92			switch child.Type {
 93				case Waypoint:
 94					@WaypointComponent(NodeToWaypointContent(child))
 95				case Text:
 96					@InlineTextComponent(NodeToTextContent(child))
 97				default:
 98					<div>unknown node type: { child.Type }</div>
 99			}
100		}
101	</p>
102}
103
104templ OrderedListComponent(attrs OrderedListAttrs, children ...Node) {
105	<ol start={ attrs.Start }>
106		for _, node := range children {
107			if node.Type != ListItem {
108				continue
109			}
110			<li>
111				@Fragment(node.Children)
112			</li>
113		}
114	</ol>
115}
116
117templ BulletListComponent(children ...Node) {
118	<ul>
119		for _, node := range children {
120			if node.Type != ListItem {
121				continue
122			}
123			<li>
124				@Fragment(node.Children)
125			</li>
126		}
127	</ul>
128}