forked from sapegin/blog.sapegin.me
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docpad.coffee
152 lines (122 loc) · 4.06 KB
/
docpad.coffee
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
# docpad.org
YAML = require 'yamljs'
moment = require 'moment'
richtypo = require 'richtypo'
# Borrowed from https://github.com/airbnb/polyglot.js/blob/master/lib/polyglot.js
pluralTypes =
en: (n) -> (if n isnt 1 then 1 else 0)
ru: (n) -> (if n % 10 is 1 and n % 100 isnt 11 then 0 else (if n % 10 >= 2 and n % 10 <= 4 and (n % 100 < 10 or n % 100 >= 20) then 1 else 2))
docpadConfig = {
# =================================
# Template Data
# These are variables that will be accessible via our templates
# To access one of these within our templates, refer to the FAQ: https://github.com/bevry/docpad/wiki/FAQ
templateData:
cutTag: '<!-- cut -->'
# Specify some site properties and local string: will be read from lang file
site: {}
# -----------------------------
# Helper Functions
# Get the prepared site/document title
# Often we would like to specify particular formatting to our page's title
# we can apply that formatting here
pageTitle: ->
# if we have a document title, then we should use that and suffix the site's title onto it
if @document.title
"#{@document.title} — #{@site.title}"
# if our document does not have it's own title, then we should just use the site's title
else
@site.title
# Get the prepared site/document description
pageDescription: ->
# if we have a document description, then we should use that, otherwise use the site's description
@document.description or @site.description
# Post part before “cut”
cuttedContent: (content) ->
if @hasReadMore content
cutIdx = content.search @cutTag
content[0..cutIdx-1]
else
content
# Has “cut”?
hasReadMore: (content) ->
content and ((content.search @cutTag) isnt -1)
# Localized date
pubDate: (date) ->
moment(date).format('LL')
# Translated string
_: (s, params=null) ->
params ?= []
s = @site[s] or s
s.replace /\{([^\}]+)\}/g, (m, key) ->
params[key] or m
# Plural form: @plural(3, 'dog|dogs')
plural: (n, s) ->
((@_ s).split '|')[pluralTypes[@site.lang](n)]
rt: (s) ->
s and (richtypo.rich s)
rtt: (s) ->
s and (richtypo.title s)
addSlashes: (s) ->
s.replace(/[\\"']/g, '\\$&').replace(/\u0000/g, '\\0')
prepareFeed: (s) ->
s and (s
.replace /href="\//g, "href=\"#{@site.url}/"
.replace /src="\//g, "src=\"#{@site.url}/"
)
# =================================
# Collections
# These are special collections that our website makes available to us
collections:
posts: (database) ->
database.findAllLive({relativeOutDirPath: 'all'}, [date:-1])
# =================================
# Environments
# Language specific configuration
# $ docpad run --env en
# $ docpad generate --env en,ru
environments:
en:
documentsPaths: ['documents_en']
outPath: 'htdocs_en'
ru:
documentsPaths: ['documents_ru']
outPath: 'htdocs_ru'
plugins:
highlightjs:
aliases:
yaml: 'python'
robotskirt:
robotskirtOptions:
EXT_AUTOLINK: false
# =================================
# DocPad Events
# Here we can define handlers for events that DocPad fires
# You can find a full listing of events on the DocPad Wiki
events:
generateBefore: (opts) ->
lang = @docpad.config.env
@docpad.getConfig().templateData.site = (YAML.load "src/lang/#{lang}.yml")
moment.lang(lang)
richtypo.lang(lang)
# Server Extend
# Used to add our own custom routes to the server before the docpad routes are added
serverExtend: (opts) ->
# Extract the server from the options
{server} = opts
docpad = @docpad
# As we are now running in an event,
# ensure we are using the latest copy of the docpad configuraiton
# and fetch our urls from it
latestConfig = docpad.getConfig()
oldUrls = latestConfig.templateData.site.oldUrls or []
newUrl = latestConfig.templateData.site.url
# Redirect any requests accessing one of our sites oldUrls to the new site url
server.use (req,res,next) ->
if req.headers.host in oldUrls
res.redirect(newUrl+req.url, 301)
else
next()
}
# Export our DocPad Configuration
module.exports = docpadConfig