Exemplo n.º 1
0
        /// <summary>
        /// Initializes new Documentor instance.
        /// REST methods will be looked for in the specified assemblies.
        /// </summary>
        public Documentor(params Assembly[] assemblies)
        {
            // set assemblies to look for REST methods
            RestApiAssemblies = assemblies;

            // initiate context
            mContext = new DocumentorContext(this);

            // default template set
            TemplateSet = new SimpleTemplateSet();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates documentation content.
        /// </summary>
        /// <returns>
        /// HTML documentation content as string.
        /// </returns>
        private string CreateDocumentation()
        {
            if (TemplateSet == null)
            {
                TemplateSet = new SimpleTemplateSet();
            }

            // take all REST methods marked with [Method] attibute
            RestMethods =
                RestApiAssemblies
                .Distinct()
                .SelectMany(a => a.GetTypes())
                .SelectMany(c => c.GetMethods()
                            .Where(m => m.IsDefined(typeof(MethodAttribute), false)));

            // methods count
            int methodsCount = RestMethods.Count();

            // order methods according to groups order and methods order in each group
            RestMethods = RestMethods.OrderBy(m =>
            {
                // get method group info from attribute
                OrderAttribute attrGroup = m.GetRadishAttribute <OrderAttribute>();

                // if group is not defined for the method, assume the method has default order "0"
                if (attrGroup == null)
                {
                    return(0);
                }

                // if group is not defined in groups list
                if (attrGroup.GroupKey != null && !MethodGroups.ContainsKey(attrGroup.GroupKey))
                {
                    string fullMethodName = string.Format("{0}.{1}()", m.ReflectedType.Name, m.Name);
                    throw new Exception(String.Format("Group with key \"{0}\" was specified for the method \"{1}\", but group with this key was not added to the Documentor. Use AddMethodGroup(\"{0}\", ...) to set it.", attrGroup.GroupKey, fullMethodName));
                }

                // if default group (group key is null or empty)
                if (String.IsNullOrEmpty(attrGroup.GroupKey))
                {
                    return(attrGroup.MethodOrder);
                }
                // group was defined
                else
                {
                    return((int)(MethodGroups[attrGroup.GroupKey].GroupOrder * methodsCount) + attrGroup.MethodOrder);
                }
            });

            // initiate rendering documentation with Page template
            return(TemplateSet.RenderTemplate <AbstractTemplateSet>(mContext, ts => ts.Page));
        }