-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpages.scala
179 lines (171 loc) · 5.77 KB
/
pages.scala
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
load.ivy("com.lihaoyi" %% "scalatags" % "0.5.3")
@
import scalatags.Text.all.{width, height, _}
import scalatags.Text._
import java.time.LocalDate
@
load.module(ammonite.ops.cwd/"styles.scala")
@
def sanitize(s: String): String = {
s.filter(_.isLetterOrDigit)
}
def pageChrome(titleText: Option[String], unNesting: String, contents: Frag): String = {
val pageTitle = titleText.getOrElse("Haoyi's Programming Blog")
val sheets = Seq(
"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css",
"https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css",
"https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.1.0/styles/github-gist.min.css"
)
val headerLinks = Seq(
div(div(i(cls:= "fa fa-question-circle")), " About") -> s"$unNesting/post/HelloWorldBlog.html",
div(div(i(cls:= "fa fa-file-text-o")), " Resume") -> "https://lihaoyi.github.io/Resume/",
div(div(i(cls:= "fa fa-github")), " Github") -> "https://github.com/lihaoyi"
)
html(
head(
meta(charset := "utf-8"),
for(sheet <- sheets)
yield link(href := sheet, rel := "stylesheet", `type` := "text/css" ),
tags2.title(pageTitle),
tags2.style(s"@media (min-width: 48em) {${WideStyles.styleSheetText}}"),
tags2.style(s"@media (max-width: 48em) {${NarrowStyles.styleSheetText}}"),
tags2.style(Styles.styleSheetText),
script(src:="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.1.0/highlight.min.js"),
script(src:="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.1.0/languages/scala.min.js"),
script(raw("hljs.initHighlightingOnLoad();")),
// This makes media queries work on iphone (???)
// http://stackoverflow.com/questions/13002731/responsive-design-media-query-not-working-on-iphone
meta(name:="viewport", content:="initial-scale = 1.0,maximum-scale = 1.0"),
googleAnalytics,
forceHttps
),
body(
margin := 0,
div(
WideStyles.header,
NarrowStyles.header,
Styles.header,
div(
NarrowStyles.headerContent,
WideStyles.headerContent,
h1(
a(
i(cls:= "fa fa-cogs"),
color := "white",
" Haoyi's Programming Blog", href := s"$unNesting",
Styles.subtleLink,
NarrowStyles.flexFont,
fontWeight.bold
),
padding := "10px 10px",
margin := 0
),
div(
Styles.headerLinkBox,
NarrowStyles.linkFlex,
for ((name, url) <- headerLinks) yield div(
Styles.headerLink,
a(name, href := url, Styles.subtleLink, color := "white")
)
)
)
),
div(
WideStyles.content,
NarrowStyles.content,
maxWidth := 900,
titleText.map(h1(_)),
contents
),
div(
WideStyles.footer,
Styles.footer,
"Last published ", currentTimeText
)
)
).render
}
val currentTimeText = LocalDate.now.toString
def commentBox(titleText: String): Frag = Seq(
div(id:="disqus_thread"),
script(raw(s"""
/**
* RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS.
* LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables
*/
/*
var disqus_config = function () {
this.page.url = "https://www.lihaoyi.com/p/$titleText"; // Replace PAGE_URL with your page's canonical URL variable
this.page.identifier = "$titleText"; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
};
*/
(function() { // DON'T EDIT BELOW THIS LINE
var d = document, s = d.createElement('script');
s.src = '//lihaoyi.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
"""))
)
def googleAnalytics: Frag = script(raw(
"""(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
|m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
|
|ga('create', 'UA-27464920-5', 'auto');
|ga('send', 'pageview');
""".stripMargin
))
def forceHttps: Frag = script(raw(
"""if (window.location.protocol != "https:")
| window.location.href = "https:" + window.location.href.substring(window.location.protocol.length);
""".stripMargin
))
def metadata(dates: Seq[(String, LocalDate)]) = div(
color := "#999",
marginBottom := 20,
"Posted ",
for ((sha, date) <- dates.lastOption) yield a(
date.toString, href := s"https://github.com/lihaoyi/site/commit/$sha"
)
)
def mainContent(posts: Seq[(String, String, String, Seq[(String, LocalDate)])]) = pageChrome(
None,
".",
div(
for((name, _, rawHtmlSnippet, dates) <- posts.reverse) yield div(
h1(a(
name,
href := s"post/${sanitize(name)}.html",
Styles.subtleLink,
color := "rgb(34, 34, 34)"
)),
metadata(dates),
raw(rawHtmlSnippet),
hr(margin := "50px 0px 50px 0px")
)
)
)
def postContent(name: String, rawHtmlContent: String, dates: Seq[(String, LocalDate)]) = pageChrome(
Some(name),
"..",
Seq[Frag](
metadata(dates),
raw(rawHtmlContent),
if (dates.length < 2) ""
else {
div(
hr,
div(
color := "rgb(158, 167, 174)",
"Updated ",
for((sha, date) <- dates.drop(1)) yield a(
date.toString, " ", href := s"https://github.com/lihaoyi/site/commit/$sha"
)
)
)
},
commentBox(name)
)
)