2014-06-26 01:56:39 -04:00
|
|
|
popoverTemplate = require 'templates/play/level/tome/spell_palette_entry_popover'
|
|
|
|
{downTheChain} = require 'lib/world/world_utils'
|
|
|
|
window.Vector = require 'lib/world/vector' # So we can document it
|
2014-11-28 20:49:41 -05:00
|
|
|
utils = require 'core/utils'
|
2014-06-26 01:56:39 -04:00
|
|
|
|
|
|
|
safeJSONStringify = (input, maxDepth) ->
|
|
|
|
recursion = (input, path, depth) ->
|
|
|
|
output = {}
|
|
|
|
pPath = undefined
|
|
|
|
refIdx = undefined
|
2014-06-30 22:16:26 -04:00
|
|
|
path = path or ''
|
2014-06-26 01:56:39 -04:00
|
|
|
depth = depth or 0
|
|
|
|
depth++
|
2014-06-30 22:16:26 -04:00
|
|
|
return '{depth over ' + maxDepth + '}' if maxDepth and depth > maxDepth
|
2014-06-26 01:56:39 -04:00
|
|
|
for p of input
|
2014-06-30 22:16:26 -04:00
|
|
|
pPath = ((if path then (path + '.') else '')) + p
|
|
|
|
if typeof input[p] is 'function'
|
|
|
|
output[p] = '{function}'
|
|
|
|
else if typeof input[p] is 'object'
|
2014-06-26 01:56:39 -04:00
|
|
|
refIdx = refs.indexOf(input[p])
|
|
|
|
if -1 isnt refIdx
|
2014-06-30 22:16:26 -04:00
|
|
|
output[p] = '{reference to ' + refsPaths[refIdx] + '}'
|
2014-06-26 01:56:39 -04:00
|
|
|
else
|
|
|
|
refs.push input[p]
|
|
|
|
refsPaths.push pPath
|
|
|
|
output[p] = recursion(input[p], pPath, depth)
|
|
|
|
else
|
|
|
|
output[p] = input[p]
|
|
|
|
output
|
|
|
|
refs = []
|
|
|
|
refsPaths = []
|
|
|
|
maxDepth = maxDepth or 5
|
2014-06-30 22:16:26 -04:00
|
|
|
if typeof input is 'object'
|
2014-06-26 01:56:39 -04:00
|
|
|
output = recursion(input)
|
|
|
|
else
|
|
|
|
output = input
|
|
|
|
JSON.stringify output, null, 1
|
|
|
|
|
|
|
|
module.exports = class DocFormatter
|
|
|
|
constructor: (@options) ->
|
2015-01-30 10:51:57 -05:00
|
|
|
@doc = _.cloneDeep @options.doc
|
2014-06-26 01:56:39 -04:00
|
|
|
@fillOutDoc()
|
|
|
|
|
|
|
|
fillOutDoc: ->
|
2016-07-17 03:53:17 -04:00
|
|
|
# TODO: figure out better ways to format html/css/scripting docs for web-dev levels
|
2014-06-26 01:56:39 -04:00
|
|
|
if _.isString @doc
|
|
|
|
@doc = name: @doc, type: typeof @options.thang[@doc]
|
|
|
|
if @options.isSnippet
|
|
|
|
@doc.type = 'snippet'
|
|
|
|
@doc.owner = 'snippets'
|
|
|
|
@doc.shortName = @doc.shorterName = @doc.title = @doc.name
|
2016-08-16 15:36:38 -04:00
|
|
|
else if @doc.owner in ['HTML', 'CSS', 'WebJavaScript', 'jQuery']
|
2016-07-14 22:14:18 -04:00
|
|
|
@doc.shortName = @doc.shorterName = @doc.title = @doc.name
|
2014-06-26 01:56:39 -04:00
|
|
|
else
|
|
|
|
@doc.owner ?= 'this'
|
|
|
|
ownerName = @doc.ownerName = if @doc.owner isnt 'this' then @doc.owner else switch @options.language
|
2016-04-16 17:59:18 -04:00
|
|
|
when 'python', 'lua' then (if @options.useHero then 'hero' else 'self')
|
2016-01-04 20:23:58 -05:00
|
|
|
when 'java' then 'hero'
|
2014-06-26 01:56:39 -04:00
|
|
|
when 'coffeescript' then '@'
|
2016-04-16 17:59:18 -04:00
|
|
|
else (if @options.useHero then 'hero' else 'this')
|
2016-07-28 16:39:58 -04:00
|
|
|
ownerName = 'game' if @options.level.isType('game-dev')
|
2014-06-26 01:56:39 -04:00
|
|
|
if @doc.type is 'function'
|
2015-03-20 16:42:41 -04:00
|
|
|
[docName, args] = @getDocNameAndArguments()
|
2016-05-24 15:00:04 -04:00
|
|
|
argNames = args.join ', '
|
2014-06-26 01:56:39 -04:00
|
|
|
argString = if argNames then '__ARGS__' else ''
|
|
|
|
@doc.shortName = switch @options.language
|
2015-03-20 16:42:41 -04:00
|
|
|
when 'coffeescript' then "#{ownerName}#{if ownerName is '@' then '' else '.'}#{docName}#{if argString then ' ' + argString else '()'}"
|
|
|
|
when 'python' then "#{ownerName}.#{docName}(#{argString})"
|
|
|
|
when 'lua' then "#{ownerName}:#{docName}(#{argString})"
|
|
|
|
else "#{ownerName}.#{docName}(#{argString});"
|
2014-06-26 01:56:39 -04:00
|
|
|
else
|
|
|
|
@doc.shortName = switch @options.language
|
|
|
|
when 'coffeescript' then "#{ownerName}#{if ownerName is '@' then '' else '.'}#{@doc.name}"
|
|
|
|
when 'python' then "#{ownerName}.#{@doc.name}"
|
|
|
|
when 'lua' then "#{ownerName}.#{@doc.name}"
|
|
|
|
else "#{ownerName}.#{@doc.name};"
|
|
|
|
@doc.shorterName = @doc.shortName
|
|
|
|
if @doc.type is 'function' and argString
|
|
|
|
@doc.shortName = @doc.shorterName.replace argString, argNames
|
2015-03-20 16:42:41 -04:00
|
|
|
@doc.shorterName = @doc.shorterName.replace argString, (if not /cast[A-Z]/.test(@doc.name) and argNames.length > 6 then '...' else argNames)
|
2014-06-26 01:56:39 -04:00
|
|
|
if @options.language is 'javascript'
|
|
|
|
@doc.shorterName = @doc.shortName.replace ';', ''
|
|
|
|
if @doc.owner is 'this' or @options.tabbify
|
2016-04-16 17:59:18 -04:00
|
|
|
@doc.shorterName = @doc.shorterName.replace /^(this|hero)\./, ''
|
2015-03-25 19:47:11 -04:00
|
|
|
else if (@options.language in ['python', 'lua']) and (@doc.owner is 'this' or @options.tabbify)
|
2016-04-16 17:59:18 -04:00
|
|
|
@doc.shorterName = @doc.shortName.replace /^(self|hero)[:.]/, ''
|
2014-06-26 01:56:39 -04:00
|
|
|
@doc.title = if @options.shortenize then @doc.shorterName else @doc.shortName
|
2016-04-25 16:20:47 -04:00
|
|
|
translatedName = utils.i18n(@doc, 'name')
|
|
|
|
if translatedName isnt @doc.name
|
|
|
|
@doc.translatedShortName = @doc.shortName.replace(@doc.name, translatedName)
|
2016-05-24 15:00:04 -04:00
|
|
|
|
2014-06-26 01:56:39 -04:00
|
|
|
|
|
|
|
# Grab the language-specific documentation for some sub-properties, if we have it.
|
2014-10-17 18:25:31 -04:00
|
|
|
toTranslate = [{obj: @doc, prop: 'description'}, {obj: @doc, prop: 'example'}]
|
2014-06-26 01:56:39 -04:00
|
|
|
for arg in (@doc.args ? [])
|
|
|
|
toTranslate.push {obj: arg, prop: 'example'}, {obj: arg, prop: 'description'}
|
2014-10-17 18:25:31 -04:00
|
|
|
if @doc.returns
|
|
|
|
toTranslate.push {obj: @doc.returns, prop: 'example'}, {obj: @doc.returns, prop: 'description'}
|
2014-06-26 01:56:39 -04:00
|
|
|
for {obj, prop} in toTranslate
|
2014-10-28 16:44:49 -04:00
|
|
|
# Translate into chosen code language.
|
2014-06-26 01:56:39 -04:00
|
|
|
if val = obj[prop]?[@options.language]
|
|
|
|
obj[prop] = val
|
|
|
|
else unless _.isString obj[prop]
|
|
|
|
obj[prop] = null
|
|
|
|
|
2014-10-28 16:44:49 -04:00
|
|
|
# Translate into chosen spoken language.
|
2015-10-05 18:31:57 -04:00
|
|
|
if val = originalVal = obj[prop]
|
2014-10-28 16:44:49 -04:00
|
|
|
context = @doc.context
|
|
|
|
obj[prop] = val = utils.i18n obj, prop
|
2014-11-23 13:21:58 -05:00
|
|
|
# For multiplexed-by-both-code-and-spoken-language objects, now also get code language again.
|
2015-10-05 18:31:57 -04:00
|
|
|
if _.isObject(val)
|
|
|
|
if valByCodeLanguage = obj[prop]?[@options.language]
|
|
|
|
obj[prop] = val = valByCodeLanguage
|
|
|
|
else
|
|
|
|
obj[prop] = originalVal # Never mind, we don't have that code language for that spoken language.
|
2014-10-28 16:44:49 -04:00
|
|
|
if @doc.i18n
|
|
|
|
spokenLanguage = me.get 'preferredLanguage'
|
|
|
|
while spokenLanguage
|
|
|
|
spokenLanguage = spokenLanguage.substr 0, spokenLanguage.lastIndexOf('-') if fallingBack?
|
|
|
|
if spokenLanguageContext = @doc.i18n[spokenLanguage]?.context
|
2014-10-29 00:15:41 -04:00
|
|
|
context = _.merge context, spokenLanguageContext
|
2014-10-28 16:44:49 -04:00
|
|
|
break
|
|
|
|
fallingBack = true
|
2014-10-29 15:08:03 -04:00
|
|
|
if context
|
|
|
|
try
|
|
|
|
obj[prop] = _.template val, context
|
|
|
|
catch e
|
|
|
|
console.error "Couldn't create docs template of", val, "\nwith context", context, "\nError:", e
|
2014-11-23 16:26:19 -05:00
|
|
|
obj[prop] = @replaceSpriteName obj[prop] # Do this before using the template, otherwise marked might get us first.
|
2014-10-28 16:44:49 -04:00
|
|
|
|
2016-04-16 17:59:18 -04:00
|
|
|
# Temporary hack to replace self|this with hero until we can update the docs
|
|
|
|
if @options.useHero
|
|
|
|
thisToken =
|
|
|
|
'python': /self/g,
|
|
|
|
'javascript': /this/g,
|
|
|
|
'lua': /self/g
|
|
|
|
|
|
|
|
if thisToken[@options.language]
|
|
|
|
if @doc.example
|
|
|
|
@doc.example = @doc.example.replace thisToken[@options.language], 'hero'
|
|
|
|
if @doc.snippets?[@options.language]?.code
|
|
|
|
@doc.snippets[@options.language].code.replace thisToken[@options.language], 'hero'
|
|
|
|
if @doc.args
|
|
|
|
arg.example = arg.example.replace thisToken[@options.language], 'hero' for arg in @doc.args when arg.example
|
|
|
|
|
2016-07-14 11:58:43 -04:00
|
|
|
if @doc.shortName is 'loop' and @options.level.isType('course', 'course-ladder')
|
2015-10-22 13:29:10 -04:00
|
|
|
@replaceSimpleLoops()
|
|
|
|
|
|
|
|
replaceSimpleLoops: ->
|
|
|
|
# Temporary hackery to make it look like we meant while True: in our loop: docs until we can update everything
|
|
|
|
@doc.shortName = @doc.shorterName = @doc.title = @doc.name = switch @options.language
|
|
|
|
when 'coffeescript' then "loop"
|
|
|
|
when 'python' then "while True:"
|
|
|
|
when 'lua' then "while true do"
|
|
|
|
else "while (true)"
|
|
|
|
for field in ['example', 'description']
|
|
|
|
[simpleLoop, whileLoop] = switch @options.language
|
|
|
|
when 'coffeescript' then [/loop/g, "loop"]
|
|
|
|
when 'python' then [/loop:/g, "while True:"]
|
|
|
|
when 'lua' then [/loop/g, "while true do"]
|
|
|
|
else [/loop/g, "while (true)"]
|
|
|
|
@doc[field] = @doc[field].replace simpleLoop, whileLoop
|
|
|
|
|
2014-06-26 01:56:39 -04:00
|
|
|
formatPopover: ->
|
2015-03-20 16:42:41 -04:00
|
|
|
[docName, args] = @getDocNameAndArguments()
|
|
|
|
argumentExamples = (arg.example or arg.default or arg.name for arg in @doc.args ? [])
|
|
|
|
argumentExamples.unshift args[0] if args.length > argumentExamples.length
|
2016-08-16 15:36:38 -04:00
|
|
|
content = popoverTemplate {
|
|
|
|
doc: @doc
|
|
|
|
docName: docName
|
|
|
|
language: @options.language
|
|
|
|
value: @formatValue()
|
|
|
|
marked: marked
|
|
|
|
argumentExamples: argumentExamples
|
|
|
|
writable: @options.writable
|
|
|
|
selectedMethod: @options.selectedMethod
|
|
|
|
cooldowns: @inferCooldowns()
|
|
|
|
item: @options.item
|
|
|
|
_: _
|
|
|
|
}
|
2014-06-26 01:56:39 -04:00
|
|
|
owner = if @doc.owner is 'this' then @options.thang else window[@doc.owner]
|
2014-11-23 16:26:19 -05:00
|
|
|
content = @replaceSpriteName content
|
2016-01-22 16:21:17 -05:00
|
|
|
content = content.replace /\#\{(.*?)\}/g, (s, properties) => @formatValue downTheChain(owner, properties.split('.'))
|
|
|
|
content = content.replace /{([a-z]+)}([^]*?){\/\1}/g, (s, language, text) =>
|
|
|
|
if language is @options.language then return text
|
|
|
|
return ''
|
2014-06-26 01:56:39 -04:00
|
|
|
|
2014-11-23 16:26:19 -05:00
|
|
|
replaceSpriteName: (s) ->
|
|
|
|
# Prefer type, and excluded the quotes we'd get with @formatValue
|
2015-01-31 15:23:34 -05:00
|
|
|
name = @options.thang.type ? @options.thang.spriteName
|
|
|
|
name = 'hero' if /Hero Placeholder/.test @options.thang.id
|
|
|
|
s.replace /#{spriteName}/g, name
|
2014-11-23 16:26:19 -05:00
|
|
|
|
2015-03-20 16:42:41 -04:00
|
|
|
getDocNameAndArguments: ->
|
|
|
|
return [@doc.name, []] unless @doc.type is 'function'
|
|
|
|
docName = @doc.name
|
|
|
|
args = (arg.name for arg in @doc.args ? [])
|
|
|
|
if /cast[A-Z]/.test docName
|
|
|
|
docName = 'cast'
|
|
|
|
args.unshift '"' + _.string.dasherize(@doc.name).replace('cast-', '') + '"'
|
|
|
|
[docName, args]
|
|
|
|
|
2014-06-26 01:56:39 -04:00
|
|
|
formatValue: (v) ->
|
2016-07-14 22:14:18 -04:00
|
|
|
return null if @options.level.isType('web-dev')
|
2014-06-26 01:56:39 -04:00
|
|
|
return null if @doc.type is 'snippet'
|
|
|
|
return @options.thang.now() if @doc.name is 'now'
|
|
|
|
return '[Function]' if not v and @doc.type is 'function'
|
|
|
|
unless v?
|
|
|
|
if @doc.owner is 'this'
|
|
|
|
v = @options.thang[@doc.name]
|
|
|
|
else
|
|
|
|
v = window[@doc.owner][@doc.name] # grab Math or Vector
|
2014-10-09 12:53:57 -04:00
|
|
|
if @doc.type is 'number' and not _.isNaN v
|
2014-11-23 00:22:46 -05:00
|
|
|
if v is Math.round v
|
2014-06-26 01:56:39 -04:00
|
|
|
return v
|
2014-10-09 12:53:57 -04:00
|
|
|
if _.isNumber v
|
|
|
|
return v.toFixed 2
|
|
|
|
unless v
|
|
|
|
return 'null'
|
|
|
|
return '' + v
|
2014-06-26 01:56:39 -04:00
|
|
|
if _.isString v
|
|
|
|
return "\"#{v}\""
|
|
|
|
if v?.id
|
|
|
|
return v.id
|
|
|
|
if v?.name
|
|
|
|
return v.name
|
|
|
|
if _.isArray v
|
|
|
|
return '[' + (@formatValue v2 for v2 in v).join(', ') + ']'
|
|
|
|
if _.isPlainObject v
|
|
|
|
return safeJSONStringify v, 2
|
|
|
|
v
|
2014-07-20 18:51:42 -04:00
|
|
|
|
|
|
|
inferCooldowns: ->
|
|
|
|
return null unless @doc.type is 'function' and @doc.owner is 'this'
|
|
|
|
owner = @options.thang
|
|
|
|
cooldowns = null
|
|
|
|
spellName = @doc.name.match /^cast(.+)$/
|
|
|
|
if spellName
|
|
|
|
actionName = _.string.slugify _.string.underscored spellName[1]
|
|
|
|
action = owner.spells?[actionName]
|
|
|
|
type = 'spell'
|
|
|
|
else
|
|
|
|
actionName = _.string.slugify _.string.underscored @doc.name
|
|
|
|
action = owner.actions?[actionName]
|
|
|
|
type = 'action'
|
|
|
|
return null unless action
|
|
|
|
cooldowns = cooldown: action.cooldown, specificCooldown: action.specificCooldown, name: actionName, type: type
|
|
|
|
for prop in ['range', 'radius', 'duration', 'damage']
|
2015-09-08 09:31:19 -04:00
|
|
|
v = owner[_.string.camelize actionName + _.string.capitalize(prop)]
|
|
|
|
continue if prop is 'range' and v <= 5 # Don't confuse players by showing melee ranges, they will inappropriately use distanceTo(enemy) < 3.
|
|
|
|
cooldowns[prop] = v
|
2014-11-23 00:22:46 -05:00
|
|
|
if _.isNumber(v) and v isnt Math.round v
|
|
|
|
cooldowns[prop] = v.toFixed 2
|
2014-07-20 18:51:42 -04:00
|
|
|
cooldowns
|