Exemplo n.º 1
0
        public static void SetRealParent(this SemanticCluster semanticCluster,
                                         SemanticGraph semanticGraph,
                                         XmlDocument xmlDoc)
        {
            var realParentNode = semanticGraph.FindCluster(semanticCluster.MediatorNodeParent)
                                 ??
                                 semanticGraph.FindNode(semanticCluster.MediatorNodeParent);

            var mediatorNodeParent = realParentNode.XmlNode ??
                                     (realParentNode is SemanticCluster cluster
                                         ? cluster.XmlNode(semanticGraph, xmlDoc)
                                         : realParentNode.XmlNode(xmlDoc));

            mediatorNodeParent.AppendChild(semanticCluster.XmlNode ?? semanticCluster.XmlNode(semanticGraph, xmlDoc));
        }
Exemplo n.º 2
0
 private static void BuildSemanticClusters(ref SemanticCluster semanticCluster, MarkdownDocument document, HeaderBlock[] headers)
 {
     foreach (var header in headers)
     {
         var subSemanticCluster = new SemanticCluster()
         {
             Parent      = semanticCluster,
             SemanticKey = header.Text(),
             Paragraph   = new Paragraph()
             {
                 OriginalText = header.Pragraph(document)?.ToString()
             }
         };
         BuildSemanticClusters(ref subSemanticCluster, document, header.SubHeaders(document));
         semanticCluster.SemanticClusters.Add(subSemanticCluster);
     }
 }
Exemplo n.º 3
0
        private static SemanticCluster Match(IEnumerable <SemanticCluster> semanticClusters, string semanticKey)
        {
            SemanticCluster match = null;

            foreach (var semanticClaster in semanticClusters)
            {
                match = semanticClaster.Match(semanticKey)
                    ? semanticClaster
                    : Match(semanticClaster.SemanticClusters, semanticKey);

                if (match != null)
                {
                    break;
                }
            }

            return(match);
        }
Exemplo n.º 4
0
        private static SemanticCluster FindCluster(IEnumerable <SemanticCluster> semanticClusters, string name)
        {
            SemanticCluster match = null;

            foreach (var semanticClaster in semanticClusters)
            {
                match = semanticClaster.Find(name)
                    ? semanticClaster
                    : FindCluster(semanticClaster.SemanticClusters, name);

                if (match != null)
                {
                    break;
                }
            }

            return(match);
        }
Exemplo n.º 5
0
        public static SemanticGraph SemanticGraph(this MarkdownDocument document)
        {
            var semanticGraph = new SemanticGraph();

            foreach (var header in document.Headers(1))
            {
                var semanticCluster = new SemanticCluster()
                {
                    Parent      = null,
                    SemanticKey = header.Text(),
                    Paragraph   = new Paragraph()
                    {
                        OriginalText = header.Pragraph(document)?.ToString()
                    }
                };

                BuildSemanticClusters(ref semanticCluster, document, header.SubHeaders(document));
                semanticGraph.SemanticClusters.Add(semanticCluster);
            }

            return(semanticGraph);
        }
Exemplo n.º 6
0
 public static SemanticCluster ChildClusterWithoutSemanticKey(this SemanticCluster semanticCluster)
 {
     return(semanticCluster.SemanticClusters.FirstOrDefault(
                cluster => string.IsNullOrWhiteSpace(cluster.SemanticKey)));
 }
Exemplo n.º 7
0
        public static XmlNode XmlNode(this SemanticCluster semanticCluster, SemanticGraph semanticGraph, XmlDocument xmlDoc)
        {
            if (semanticCluster.XmlGenerated())
            {
                return(semanticCluster.XmlNode);
            }

            var nodeName = semanticCluster.NodeNameIsNodeValue
                ? semanticCluster.Value
                : semanticCluster.Name;

            if (semanticCluster.Type == MediatorNodeType.attribute ||
                string.IsNullOrWhiteSpace(nodeName))
            {
                return(null);
            }

            semanticCluster.XmlNode = xmlDoc.CreateElement(nodeName);

            foreach (var childClusterNode in semanticCluster.SemanticClusters)
            {
                if (childClusterNode.Type == MediatorNodeType.attribute)
                {
                    throw new Exception($"The cluster {semanticCluster.SemanticKey} - {semanticCluster.Name} type cannot be attribute.");
                }

                if (childClusterNode.HasDifferentParent())
                {
                    childClusterNode.SetRealParent(semanticGraph, xmlDoc);
                }
                else
                {
                    var newChildNode = childClusterNode.XmlNode(semanticGraph, xmlDoc);
                    if (newChildNode != null)
                    {
                        semanticCluster.XmlNode.AppendChild(newChildNode);
                    }
                }
            }

            foreach (var childSemanticNode in semanticCluster.SemanticNodes)
            {
                if (childSemanticNode.Type == MediatorNodeType.attribute)
                {
                    semanticCluster.XmlNode.Attributes?.Append(childSemanticNode.XmlAttribute(xmlDoc));
                }
                else if (childSemanticNode.HasDifferentParent())
                {
                    childSemanticNode.SetRealParent(semanticGraph, xmlDoc);
                }
                else
                {
                    var newChildNode = childSemanticNode.XmlNode(xmlDoc);
                    if (newChildNode != null)
                    {
                        semanticCluster.XmlNode.AppendChild(newChildNode);
                    }
                }
            }

            return(semanticCluster.XmlNode);
        }
Exemplo n.º 8
0
 public static SemanticCluster ChildClusterWithoutMediatorParent(this SemanticCluster semanticCluster)
 {
     return(semanticCluster.SemanticClusters.FirstOrDefault(
                cluster => string.IsNullOrWhiteSpace(cluster.MediatorNodeParent)));
 }