   http = require 'http'
   docs = require 'graphite_docs'
   parser = require 'graphite_parser'
-
-  graphite = modules.create 'graphite', ({fn, cmd, settings}) ->
+  builtins = require 'builtins'
+
+  graphite = modules.create 'graphite', ({fn, component_fn, cmd, settings}) ->
     args_to_params = (context, args) ->
       graphite.args_to_params {args, default_options: context.options()}
 

        name = name._lead_context_fn?.name if name._lead_op?
        function_docs = docs.function_docs[name]
        if function_docs?
          @div function_docs.docs
          for example in function_docs.examples
            @example "#{default_target_command} #{JSON.stringify example}", run: false
        name = docs.parameter_doc_ids[name] ? name
        parameter_docs = docs.parameter_docs[name]
        if parameter_docs?
          $docs = $(parameter_docs)
          context = @
          $docs.find('a').on 'click', (e) ->
            e.preventDefault()
            href = $(this).attr 'href'
            if href[0] is '#'
              context.run "docs '#{decodeURI href[1..]}'"
          @div $docs
        unless function_docs? or parameter_docs?
          @text 'Documentation not found'
      else
        @html '<h3>Functions</h3>'
        names = (name for name of docs.function_docs)
        names.sort()
        for name in names
          item = docs.function_docs[name]
          @example "docs #{name}  # #{item.signature}"

        @html '<h3>Parameters</h3>'
        names = (name for name of docs.parameter_docs)
        names.sort()
        for name in names
          @example "docs '#{name}'"

    fn 'params', 'Generates the parameters for a Graphite render call', (args...) ->
      result = args_to_params @, args
      @value result

    fn 'url', 'Generates a URL for a Graphite image', (args...) ->
      params = args_to_params @, args
      url = graphite.render_url params
      $a = $ "<a href='#{url}' target='blank'/>"
      $a.text url
      $pre = $ '<pre>'
      $pre.append $a
      @add_rendered $pre

    fn 'img', 'Renders a Graphite graph image', (args...) ->
      params = args_to_params @, args
      url = graphite.render_url params
      @async ->
        $img = $ "<img src='#{url}'/>"
        @div $img
        deferred = Q.defer()

        unless _.isString t
          throw new TypeError "#{t} is not a string"
      @value new dsl.type.q targets.map(String)...

    DocsIndex = React.createClass
      render: ->
        names = (name for name of @props.docs)
        names.sort()
        React.DOM.div {},
          _.map names, (name) =>
            signature = @props.docs[name].signature
            name = JSON.stringify name if @props.quote
            value = "docs #{name}"
            value += " # #{signature}" if signature?
            builtins.ExampleComponent ctx: @props.ctx, value: value, run: true

    FunctionDocsComponent = React.createClass
      render: ->
        React.DOM.div {}, [
          React.DOM.div dangerouslySetInnerHTML: __html: @props.docs.docs
          _.map @props.docs.examples, (example) =>
            builtins.ExampleComponent ctx: @props.ctx, value: "#{default_target_command} #{JSON.stringify example}", run: false
        ]

    ParameterDocsComponent = React.createClass
      render: -> React.DOM.div()
      componentDidMount: (node) ->
        ctx = @props.ctx
        # TODO
        $docs = $(node).append @props.docs
        $docs.find('a').on 'click', (e) ->
          e.preventDefault()
          href = $(this).attr 'href'
          if href[0] is '#'
            ctx.run "docs '#{decodeURI href[1..]}'"

    cmd 'docs', 'Shows the documentation for a graphite function or parameter', (name) ->
      if name?
