forked from meridius/confluence-to-markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Formatter.coffee
238 lines (199 loc) · 6.37 KB
/
Formatter.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
class Formatter
###*
# @param {cheerio} _cheerio Required lib
# @param {Utils} utils My lib
# @param {Logger} logger My lib
###
constructor: (@_cheerio, @utils, @logger) ->
###*
# @param {string} text Content of a file
# @return {cheerio obj} Root object of a text
###
load: (text) ->
@_cheerio.load text
###*
# @param {cheerio obj} $content Content of a file
# @return {string} Textual representation of a content
###
getText: ($content) ->
$content.text()
###*
# @param {cheerio obj} $content Content of a file
# @return {string} HTML representation of a content
###
getHtml: ($content) ->
$ = @_cheerio
contentHtml = ''
$content.each (i, el) =>
contentHtml += $(el).html()
contentHtml
###*
# The right content is selected based on the filename given.
# Actual content of a page is placed elsewhere for index.html and other pages.
# @see load() You need to load the content first.
# @param {string} fileName Name of a file
###
getRightContentByFileName: ($content, fileName) ->
if fileName == 'index.html'
$content.find('#content')
.find('#main-content>.confluenceTable').remove().end() # Removes arbitrary table located on top of index page
else
selector = [
'#main-content'
'.pageSection.group:has(.pageSectionHeader>#attachments)'
'.pageSection.group:has(.pageSectionHeader>#comments)'
]
$content.find selector.join ', '
###*
# Removes span inside of a h1 tag.
# @param {cheerio obj} $content Content of a file
# @return {cheerio obj} Cheerio object
###
fixHeadline: ($content) ->
@_removeElementLeaveText $content, 'span.aui-icon'
addPageHeading: ($content, headingText) ->
$ = @_cheerio
h1 = $('<h1>').text headingText
$content.first().prepend h1
$content
###*
# Removes redundant icon
# @param {cheerio obj} $content Content of a file
# @return {cheerio obj} Cheerio object
###
fixIcon: ($content) ->
@_removeElementLeaveText $content, 'span.aui-icon'
###*
# Removes empty link
# @param {cheerio obj} $content Content of a file
# @return {cheerio obj} Cheerio object
###
fixEmptyLink: ($content) ->
$ = @_cheerio
$content
.find('a').each (i, el) =>
if (
$(el).text().trim().length == 0 \
and $(el).find('img').length == 0
)
$(el).remove()
.end()
###*
# Removes empty heading
# @param {cheerio obj} $content Content of a file
# @return {cheerio obj} Cheerio object
###
fixEmptyHeading: ($content) ->
$ = @_cheerio
$content
.find(':header').each (i, el) =>
if $(el).text().trim().length == 0
$(el).remove()
.end()
###*
# Gives the right class to syntaxhighlighter
# @param {cheerio obj} $content Content of a file
# @return {cheerio obj} Cheerio object
###
fixPreformattedText: ($content) ->
$ = @_cheerio
$content
.find('pre').each (i, el) =>
data = $(el).data('syntaxhighlighterParams')
$(el).attr('style', data)
styles = $(el).css()
brush = styles?.brush
$(el).removeAttr 'class'
$(el).addClass brush if brush
.end()
###*
# Fixes 'p > a > span > img' for which no image was created.
# @param {cheerio obj} $content Content of a file
# @return {cheerio obj} Cheerio object
###
fixImageWithinSpan: ($content) ->
$ = @_cheerio
$content
.find('span:has(img)').each (i, el) =>
if $(el).text().trim().length == 0
$(el).replaceWith($(el).html())
.end()
removeArbitraryElements: ($content) ->
@_removeElementLeaveText $content, 'span, .user-mention'
###*
# Removes arbitrary confluence classes.
# @param {cheerio obj} $content Content of a file
# @return {cheerio obj} Cheerio object
###
fixArbitraryClasses: ($content) ->
$content
.find('*').removeClass (i, e) ->
(
e.match(/(^|\s)(confluence\-\S+|external-link|uri|tablesorter-header-inner|odd|even|header)/g) || []
).join ' '
.end()
###*
# Removes arbitrary confluence elements for attachments.
# @param {cheerio obj} $content Content of a file
# @return {cheerio obj} Cheerio object
###
fixAttachmentWraper: ($content) ->
$content
.find('.attachment-buttons').remove().end() # action buttons for attachments
.find('.plugin_attachments_upload_container').remove().end() # dropbox for uploading new files
.find('table.attachments.aui').remove().end() # overview table with useless links
###*
# Removes arbitrary confluence elements for page log.
# @param {cheerio obj} $content Content of a file
# @return {cheerio obj} Cheerio object
###
fixPageLog: ($content) ->
$content
.find('[id$="Recentspaceactivity"], [id$=Spacecontributors]').parent().remove()
.end().end()
###*
# Changes links to local HTML files to generated MD files.
# @param {cheerio obj} $content Content of a file
# @param {string} cwd Current working directory (where HTML file reside)
# @return {cheerio obj} Cheerio object
###
fixLocalLinks: ($content, space, pages) ->
$ = @_cheerio
$content
.find('a').each (i, el) =>
href = $(el).attr 'href'
if href == undefined
text = $(el).text()
$(el).replaceWith text
@logger.debug 'No href for link with text "#{text}"'
else if $(el).hasClass 'createlink'
$(el).replaceWith $(el).text()
else if pageLink = @utils.getLinkToNewPageFile href, pages, space
$(el).attr 'href', pageLink
.end()
###*
# @param {array} indexHtmlFiles Relative paths of index.html files from all parsed Confluence spaces
# @return {cheerio obj} Cheerio object
###
createListFromArray: (itemArray) ->
$ = @_cheerio.load '<ul>'
$ul = $('ul')
for item in itemArray
$a = $('<a>').attr('href', item).text item.replace '/index', ''
$li = $('<li>')
$li.append $a
$ul.append $li
$ul.end()
###*
# Removes element by selector and leaves only its text content
# @param {cheerio obj} $content Content of a file
# @param {string} selector Selector of an element
# @return {cheerio obj} Cheerio object
###
_removeElementLeaveText: ($content, selector) ->
$ = @_cheerio
$content
.find(selector).each (i, el) =>
$(el).replaceWith $(el).text()
.end()
module.exports = Formatter