Esempio n. 1
0
        public InlineCode ToCode(InlineCode separatorCode)
        {
            var code = separatorCode + Code;

            if (Conditional != null)
            {
                code = new InlineCode(
                    Expression.IfThen(
                        Conditional,
                        new CodeBlock(code)));
            }

            return(Preamble + code);
        }
Esempio n. 2
0
        static InlineCode WriteNodeProperties(
            CodeGenerationContext codeGenContext,
            List <NodeProperty> nodeProperties)
        {
            nodeProperties = nodeProperties.OrderBy(p => p.Name).ToList();
            var alwaysWrittenProperties = nodeProperties.Where(p => p.Conditional == null).ToList();
            var conditionalProperties   = nodeProperties.Where(p => p.Conditional != null).ToList();

            IEnumerable <AnyExpression> renderNode()
            {
                var sortedProperties = alwaysWrittenProperties.Concat(conditionalProperties).ToArray();

                // tricky code ahead. If there are always written props, we optimise by simply always writing the separator
                // except for the first.
                // If not, we generate the tracking var.
                if (alwaysWrittenProperties.Any())
                {
                    for (var index = 0; index < sortedProperties.Length; index++)
                    {
                        yield return(sortedProperties[index].ToCode(index == 0
              ? null
              : new InlineCode(codeGenContext.JsonWriter.WriteValueSeparator())));
                    }
                }
                else
                {
                    var propAlreadyWritten = New.Var <bool>("propAlreadyWritten");
                    yield return(propAlreadyWritten);

                    yield return(propAlreadyWritten.Assign(false));

                    var separator = new InlineCode(
                        If.ThenElse(
                            propAlreadyWritten.EqualTo(true),
                            Expression.Block(codeGenContext.JsonWriter.WriteValueSeparator()),
                            propAlreadyWritten.Assign(true))
                        );
                    foreach (var nodeProp in sortedProperties)
                    {
                        yield return(nodeProp.ToCode(separator));
                    }
                }
            }

            return(new InlineCode(renderNode()));
        }
Esempio n. 3
0
        public static string ConvertPairedMarkdownToHtml(this string input, string markdownTag, MarkdownParser.PairedMarkdownTags tag)
        {
            try
            {
                string output   = string.Empty;
                var    tagSplit = input.Split(new string[] { markdownTag }, StringSplitOptions.None);

                for (int i = 0; i < tagSplit.Length; i++)
                {
                    if (i != 0 && i != tagSplit.Length - 1)
                    {
                        if (tagSplit[i][0] != ' ' && tagSplit[i][tagSplit[i].Length - 1] != ' ')
                        {
                            if (!(tagSplit[i][0] == '~' || tagSplit[i][0] == '_' || tagSplit[i][0] == '*' ||
                                  tagSplit[i][tagSplit[i].Length - 1] == '~' || tagSplit[i][tagSplit[i].Length - 1] == '_' || tagSplit[i][tagSplit[i].Length - 1] == '*'))
                            {
                                if (tag == MarkdownParser.PairedMarkdownTags.Bold)
                                {
                                    tagSplit[i] = new Bold(tagSplit[i]).ToString();
                                }
                                else if (tag == MarkdownParser.PairedMarkdownTags.Italic)
                                {
                                    tagSplit[i] = new Italic(tagSplit[i]).ToString();
                                }
                                else if (tag == MarkdownParser.PairedMarkdownTags.InlineCode)
                                {
                                    tagSplit[i] = new InlineCode(tagSplit[i]).ToString();
                                }
                                else if (tag == MarkdownParser.PairedMarkdownTags.StrikeThrough)
                                {
                                    tagSplit[i] = new StrikeThrough(tagSplit[i]).ToString();
                                }
                            }
                        }
                    }
                    output += tagSplit[i];
                }

                return(output);
            }
            catch (Exception e)
            {
                throw new Exception("An error occured while converting paired Markdown to HTML. " + e.Message);
            }
        }
Esempio n. 4
0
        static InlineCode WriteNodeProperties(
            Variable <JsonWriter> jsonWriter,
            List <NodeProperty> nodeProperties)
        {
            nodeProperties = nodeProperties.OrderBy(p => p.Name).ToList();
            var alwaysWrittenProperties = nodeProperties.Where(p => p.Conditional == null).ToList();
            var conditionalProperties   = nodeProperties.Where(p => p.Conditional != null).ToList();

            IEnumerable <AnyExpression> renderNode()
            {
                InlineCode separator;

                if (alwaysWrittenProperties.Any())
                {
                    separator = new InlineCode(jsonWriter.WriteValueSeparator());
                }
                else
                {
                    var hasProp = New.Var <bool>("hasProp");

                    yield return(hasProp);

                    yield return(hasProp.Assign(false));

                    separator = new InlineCode(
                        If.Then(
                            hasProp.EqualTo(false),
                            Expression.Block(jsonWriter.WriteValueSeparator(),
                                             hasProp.Assign(true))
                            ));
                }

                var sortedProperties = alwaysWrittenProperties.Concat(conditionalProperties).ToArray();

                for (var index = 0; index < sortedProperties.Length; index++)
                {
                    yield return(sortedProperties[index].ToCode(index == 0 ? null : separator));
                }
            }

            return(new InlineCode(renderNode()));
        }
Esempio n. 5
0
        static NodeProperty CreateNodeProperty(
            CompilerContext compilerContext,
            CodeGenerationContext codeGenContext,
            ResourceProperty property)
        {
            Expression resource = codeGenContext.ResourceInstance;

            if (codeGenContext.ResourceInstance != resource)
            {
                throw new InvalidOperationException("yo backtrack");
            }

            var pi = property.Member;

            // var propertyValue;
            Debug.Assert(pi.DeclaringType != null, "pi.DeclaringType != null");
            var propertyValue = Expression.Variable(pi.PropertyType, $"val{pi.DeclaringType.Name}{pi.Name}");

            // propertyValue = resource.Property;
            var propertyValueAssignment = Expression.Assign(propertyValue, Expression.MakeMemberAccess(resource, pi));

            var preamble = new InlineCode(new Expression[] { propertyValue, propertyValueAssignment });

            if (compilerContext.MetaModel.TryGetResourceModel(pi.PropertyType, out var propertyResourceModel))
            {
                var jsonPropertyName = property.Name;
                return(new NodeProperty(jsonPropertyName)
                {
                    Preamble = preamble,
                    Code = new InlineCode(new[]
                    {
                        codeGenContext.JsonWriter.WritePropertyName(jsonPropertyName),
                        codeGenContext.JsonWriter.WriteBeginObject(),
                        WriteNode(
                            compilerContext.Push(propertyResourceModel),
                            codeGenContext.Push(propertyValue)),
                        codeGenContext.JsonWriter.WriteEndObject()
                    }),
                    Conditional = Expression.NotEqual(propertyValue, Expression.Default(pi.PropertyType))
                });
            }

            var itemTypes = (from i in pi.PropertyType.GetInterfaces()
                             .Concat(pi.PropertyType.IsInterface ? new[] { pi.PropertyType } : Array.Empty <Type>())
                             where i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IEnumerable <>)
                             let itemType = i.GetGenericArguments()[0]
                                            where itemType != typeof(object)
                                            select itemType).ToList();

            // not an iri node itself, but is it a list of nodes?
            var itemResourceRegistrations = (
                from itemType in itemTypes
                let resourceModels = compilerContext.MetaModel.ResourceRegistrations.Where(r =>
                                                                                           r.ResourceType != null && itemType.IsAssignableFrom(r.ResourceType)).ToList()
                                     where resourceModels.Any()
                                     orderby resourceModels.Count() descending
                                     select
                                     (
                    itemType,
                    (from possible in resourceModels
                     orderby possible.ResourceType.GetInheritanceDistance(itemType)
                     select possible).ToList()
                                     )).ToList <(Type itemType, List <ResourceModel> models)>();

            if (itemResourceRegistrations.Any())
            {
                return(CreateNodePropertyAsList(compilerContext, codeGenContext, property, itemResourceRegistrations,
                                                propertyValue, preamble));
            }

            // not a list of iri or blank nodes
            var propValue = CreateNodePropertyValue(codeGenContext, property, resource);

            propValue.Preamble    = preamble;
            propValue.Conditional = Expression.NotEqual(propertyValue, Expression.Default(pi.PropertyType));
            return(propValue);

            // it's a list of nodes
        }
Esempio n. 6
0
 /// <summary>
 /// Represents a character in inline code.
 /// </summary>
 public InlineCodeCharacter(MarkdownDocument Document, InlineCode Source, char Character)
     : base(Document, Source, Character)
 {
 }
Esempio n. 7
0
 public virtual void Visit(InlineCode inline)
 {
     Write("<code>{0}</code>", Escape(inline.Code));
 }
Esempio n. 8
0
 private string FormatInlineCode(InlineCode block)
 {
     return("<code>" + block.Text + "</code>");
 }
Esempio n. 9
0
 private void WriteInlineCode(InlineCode obj)
 {
     Writer.Write("<code class=\"inline\">{0}</code>", obj.Code);
 }
Esempio n. 10
0
        static NodeProperty WriteNodeProperty(
            Variable <JsonWriter> jsonWriter,
            TypedExpression <string> baseUri,
            Expression resource,
            MemberAccess <Func <object, string> > uriGenerator,
            MemberAccess <Func <object, string> > typeGenerator,
            IMetaModelRepository models,
            Stack <ResourceModel> recursionDefender,
            PropertyInfo pi,
            Variable <HydraJsonFormatterResolver> jsonFormatterResolver)
        {
            // var propertyValue;
            var propertyValue = Expression.Variable(pi.PropertyType, $"val{pi.DeclaringType.Name}{pi.Name}");

            // propertyValue = resource.Property;
            var propertyValueAssignment = Expression.Assign(propertyValue, Expression.MakeMemberAccess(resource, pi));

            var preamble = new InlineCode(new Expression[] { propertyValue, propertyValueAssignment });

            if (models.TryGetResourceModel(pi.PropertyType, out var propertyResourceModel))
            {
                var jsonPropertyName = HydraTextExtensions.GetJsonPropertyName(pi);
                return(new NodeProperty(jsonPropertyName)
                {
                    Preamble = preamble,
                    Code = new InlineCode(new[]
                    {
                        jsonWriter.WritePropertyName(jsonPropertyName),
                        jsonWriter.WriteBeginObject(),
                        WriteNode(jsonWriter, baseUri, propertyResourceModel, propertyValue,
                                  uriGenerator, typeGenerator, models, jsonFormatterResolver, recursionDefender: recursionDefender),
                        jsonWriter.WriteEndObject()
                    }),
                    Conditional = Expression.NotEqual(propertyValue, Expression.Default(pi.PropertyType))
                });
            }

            var itemTypes = (from i in pi.PropertyType.GetInterfaces()
                             .Concat(pi.PropertyType.IsInterface ? new[] { pi.PropertyType } : Array.Empty <Type>())
                             where i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IEnumerable <>)
                             let itemType = i.GetGenericArguments()[0]
                                            where itemType != typeof(object)
                                            select itemType).ToList();

            // not an iri node itself, but is it a list of nodes?
            var itemResourceRegistrations = (
                from itemType in itemTypes
                let resourceModels = models.ResourceRegistrations.Where(r =>
                                                                        r.ResourceType != null && itemType.IsAssignableFrom(r.ResourceType)).ToList()
                                     where resourceModels.Any()
                                     orderby resourceModels.Count() descending
                                     select
                                     (
                    itemType,
                    (from possible in resourceModels
                     orderby possible.ResourceType.GetInheritanceDistance(itemType)
                     select possible).ToList()
                                     )).ToList <(Type itemType, List <ResourceModel> models)>();

            if (itemResourceRegistrations.Any() == false)
            {
                // not a list of iri or blank nodes
                var propValue = WriteNodePropertyValue(jsonWriter, pi, jsonFormatterResolver, resource);
                propValue.Preamble    = preamble;
                propValue.Conditional = Expression.NotEqual(propertyValue, Expression.Default(pi.PropertyType));
                return(propValue);
            }

            // it's a list of nodes
            return(WriteNodeList(jsonWriter, baseUri, uriGenerator, typeGenerator, models, recursionDefender, pi,
                                 jsonFormatterResolver,
                                 itemResourceRegistrations, propertyValue, preamble));
        }
 public virtual void Visit(InlineCode inline)
 {
     WriteStartTag(inline, "code");
     WriteEscaped(inline.Code);
     WriteEndTag(inline, "code");
 }