This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 70
/
fetch-property-docs.coffee
85 lines (70 loc) · 2.72 KB
/
fetch-property-docs.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
path = require 'path'
fs = require 'fs'
request = require 'request'
mdnCSSURL = 'https://developer.mozilla.org/en-US/docs/Web/CSS'
mdnJSONAPI = 'https://developer.mozilla.org/en-US/search.json?topic=css&highlight=false'
PropertiesURL = 'https://raw.githubusercontent.com/adobe/brackets/master/src/extensions/default/CSSCodeHints/CSSProperties.json'
fetch = ->
propertiesPromise = new Promise (resolve) ->
request {json: true, url: PropertiesURL}, (error, response, properties) ->
if error?
console.error(error.message)
resolve(null)
if response.statusCode isnt 200
console.error("Request for CSSProperties.json failed: #{response.statusCode}")
resolve(null)
resolve(properties)
propertiesPromise.then (properties) ->
return unless properties?
MAX = 10
queue = Object.keys(properties)
running = []
docs = {}
new Promise (resolve) ->
checkEnd = ->
resolve(docs) if queue.length is 0 and running.length is 0
removeRunning = (propertyName) ->
index = running.indexOf(propertyName)
running.splice(index, 1) if index > -1
runNext = ->
checkEnd()
if queue.length isnt 0
propertyName = queue.pop()
running.push(propertyName)
run(propertyName)
run = (propertyName) ->
url = "#{mdnJSONAPI}&q=#{propertyName}"
request {json: true, url}, (error, response, searchResults) ->
if not error? and response.statusCode is 200
handleRequest(propertyName, searchResults)
else
console.error "Req failed #{url}; #{response.statusCode}, #{error}"
removeRunning(propertyName)
checkEnd()
runNext()
handleRequest = (propertyName, searchResults) ->
if searchResults.documents?
for doc in searchResults.documents
if doc.url is "#{mdnCSSURL}/#{propertyName}"
docs[propertyName] = filterExcerpt(propertyName, doc.excerpt)
break
return
runNext() for [0..MAX]
return
filterExcerpt = (propertyName, excerpt) ->
beginningPattern = /^the (css )?[a-z-]+ (css )?property (is )?(\w+)/i
excerpt = excerpt.replace beginningPattern, (match) ->
matches = beginningPattern.exec(match)
firstWord = matches[4]
firstWord[0].toUpperCase() + firstWord.slice(1)
periodIndex = excerpt.indexOf('.')
excerpt = excerpt.slice(0, periodIndex + 1) if periodIndex > -1
excerpt
# Save a file if run from the command line
if require.main is module
fetch().then (docs) ->
if docs?
fs.writeFileSync(path.join(__dirname, 'property-docs.json'), "#{JSON.stringify(docs, null, ' ')}\n")
else
console.error 'No docs'
module.exports = fetch