Пример #1
0
		public void Add(DocumentNode node)
		{
			node.ParentFolder = owner;
			
			if (node.NodeType == NodeType.Navigation)
			{
				navNode = node;
			}
			else if (node.NodeType == NodeType.Index)
			{
				indexNode = node;
			}
			
			list.Add(node.Meta.Order, node);
		}
Пример #2
0
		private void FixRelativePaths(DocumentNode node)
		{
			if (node.XmlDoc == null) return;

			int level = node.ParentFolder.Level;

			XmlNodeList nodes = node.XmlDoc.SelectNodes("//@relative");

			foreach(XmlAttribute xmlNode in nodes)
			{
				XmlElement elem = xmlNode.OwnerElement;

				String relative = elem.GetAttribute("relative");

				if (relative.StartsWith("!"))
				{
					relative = relative.Substring(1);
					elem.SetAttribute("relative", relative);
				}
				else
				{
					elem.RemoveAttribute("relative");
				}

				if (elem.Name.ToLower() == "a")
				{
					elem.SetAttribute("href", Relativize(level, relative));
				}
				else
				{
					elem.SetAttribute("src", Relativize(level, relative));
				}
			}
		}
Пример #3
0
		private void CreateNodeSiteMapFragments(DocumentNode node)
		{
			if (node.NodeType == NodeType.Navigation) return;

			try
			{
				XmlDocumentFragment fragment = node.XmlDoc.CreateDocumentFragment();

				XmlNode parent = fragment;

				int level = node.ParentFolder.Level;

				if (node.NodeType == NodeType.Ordinary)
				{
					level++;

					parent = node.XmlDoc.CreateElement(Path.GetFileNameWithoutExtension(node.Filename).Replace(' ', '_'));

					((XmlElement) parent).SetAttribute("level", node.ParentFolder.Level.ToString());
					((XmlElement) parent).SetAttribute("page", node.TargetFilename);
					((XmlElement) parent).SetAttribute("title", node.Meta.Title);
					((XmlElement) parent).SetAttribute("issubpage", "true");
					((XmlElement) parent).SetAttribute("path", node.ParentFolder.Path);

					fragment.AppendChild(parent);
				}

				foreach(XmlElement section in node.XmlDoc.SelectNodes("document/body/section"))
				{
					XmlElement newSection = CreateSectionXmlElement(level, node, section);

					parent.AppendChild(newSection);

					foreach(XmlElement secSectionLevel in section.SelectNodes("section"))
					{
						XmlElement newSectionSecLevel = CreateSectionXmlElement(level + 1, node, secSectionLevel);

						newSection.AppendChild(newSectionSecLevel);

						foreach(XmlElement thirdSectionLevel in secSectionLevel.SelectNodes("section"))
						{
							XmlElement newSectionThrdLevel = CreateSectionXmlElement(level + 2, node, thirdSectionLevel);

							newSectionSecLevel.AppendChild(newSectionThrdLevel);
						}
					}
				}

				node.ParentFolder.SectionFragments.Add(fragment);
			}
			catch(Exception ex)
			{
				throw new Exception("Error creating site map fragments for " + node.Path + "\\" + node.Filename, ex);
			}
		}
Пример #4
0
		private static XmlElement CreateSectionXmlElement(int level, DocumentNode node, XmlElement section)
		{
			String sectionId = section.GetAttribute("id");
			XmlNode titleElem = section.SelectSingleNode("title");
			String title = titleElem.FirstChild.Value;

			XmlElement newSection = node.XmlDoc.CreateElement("section");

			if (sectionId != String.Empty)
			{
				newSection.SetAttribute("id", sectionId);
			}
			if (title != null)
			{
				newSection.SetAttribute("title", title);
			}

			newSection.SetAttribute("page", node.TargetFilename);
			newSection.SetAttribute("level", level.ToString());

			return newSection;
		}
Пример #5
0
		private void CreateBreadCrumb(DocumentNode node)
		{
			if (node.NodeType == NodeType.Navigation) return;

			EnsureFolderHasBreadCrumb(node.ParentFolder);

			node.BreadCrumbs.AddRange(node.ParentFolder.BreadCrumbs);

			if (node.NodeType != NodeType.Index)
			{
				node.BreadCrumbs.Add(new BreadCrumb("", node.Meta.Title));
			}
		}
Пример #6
0
		private IContext CreateContext(DocumentNode node)
		{
			VelocityContext context = new VelocityContext();

			context.Put("cs", csharpFormatter);
			context.Put("html", htmlFormatter);
			context.Put("js", jsFormatter);
			context.Put("tsql", tsqlFormatter);
			context.Put("vb", vbFormatter);

			context.Put("basefolder", BaseGenFolder);
			context.Put("breadcrumbs", node.BreadCrumbs);
			context.Put("meta", node.Meta);
			context.Put("doc", node.XmlDoc);
			context.Put("root", node.XmlDoc.DocumentElement);
			context.Put("node", node);
			context.Put("sitemap", siteMapDoc.DocumentElement);
			context.Put("project", projectDom.DocumentElement);
			context.Put("folder", node.ParentFolder);
			context.Put("converter", new SimpleConverter());
			context.Put("helper", new SimpleHelper());
			context.Put("generationdate", DateTime.Now);

			if (node.ParentFolder.NavigationNode != null)
			{
				context.Put("navigation", node.ParentFolder.NavigationNode.XmlDoc);
			}

			context.Put("navlevel", node.ParentFolder.NavigationLevel);

			String relativePath = String.Empty;

			if (node.ParentFolder.Level == 1)
			{
				relativePath = ".";
			}
			else
			{
				for(int i = 0; i < node.ParentFolder.Level - 1; i++)
				{
					if (i == 0)
					{
						relativePath += "..";
					}
					else
					{
						relativePath += "/..";
					}
				}
			}

			context.Put("relativePath", relativePath);

			return context;
		}
Пример #7
0
		private void CreateHtml(DocumentNode node)
		{
			try
			{
				EnsureDirExists(node.ParentFolder.Path);

				if (node.NodeType == NodeType.Navigation) return;

				DirectoryInfo targetFolder = new DirectoryInfo(
					targetDir.FullName + "/" + node.ParentFolder.Path);

				FileInfo htmlFileInTarget = new FileInfo(
					Path.Combine(targetFolder.FullName, node.TargetFilename));

				if (htmlFileInTarget.Exists)
				{
					htmlFileInTarget.Delete();
				}

				IContext ctx = CreateContext(node);

				using(StreamWriter writer = new StreamWriter(htmlFileInTarget.FullName, false))
				{
					template.Merge(ctx, writer);
				}
			}
			catch(Exception ex)
			{
				throw new Exception("Error generating html for " + node.TargetFilename +
				                    " at " + node.ParentFolder.Path, ex);
			}
		}
Пример #8
0
		private void AssignNavigationDocToFolders(DocumentNode node)
		{
			if (node.NodeType != NodeType.Navigation)
			{
				return;
			}

			node.ParentFolder.NavigationNode = node;
		}
Пример #9
0
		protected override void ExecuteTask()
		{
			projectDom = new XmlDocument();
			projectDom.Load(projectFile);

			DirectoryInfo basedir = sourceFileSet.BaseDirectory;

			root = new Folder("castle");

			try
			{
				ArrayList staticFilesToCopy = new ArrayList();

				foreach(String fullFileName in sourceFileSet.FileNames)
				{
					string lastProcessedFile = null;

					try
					{
						lastProcessedFile = fullFileName;

						String dir = Path.GetDirectoryName(fullFileName);
						String fileName = Path.GetFileName(fullFileName);
						String nodeName = String.Empty;

						Folder folder;
						String[] folders;

						if (basedir.FullName.ToLower() != dir.ToLower())
						{
							nodeName = dir.Substring(basedir.FullName.Length + 1);
						}

						if (IsStaticFile(fullFileName))
						{
							if (nodeName != String.Empty)
							{
								staticFilesToCopy.Add(new FileToCopy(
								                      	fullFileName, root.Name + "/" + nodeName + "/" + fileName));
							}
							else
							{
								staticFilesToCopy.Add(new FileToCopy(
								                      	fullFileName, root.Name + "/" + fileName));
							}

							continue;
						}

						if (nodeName != String.Empty)
						{
							folders = nodeName.Split('\\');
							folder = GetFolderInstance(folders);
						}
						else
						{
							folder = root;
						}

						XmlDocument doc = new XmlDocument();

						doc.Load(fullFileName);

						DocumentNode node = new DocumentNode(nodeName, fileName, doc, CreateMeta(doc));
						folder.Documents.Add(node);
					}
					catch(Exception ex)
					{
						Console.WriteLine("File: {0} \r\n", lastProcessedFile);
						Console.WriteLine(ex);
						Console.WriteLine("\r\n --------------------------------------------------------");
					}
				}

				siteMapDoc = CreateSiteMap();

				siteMapDoc.Save(Path.Combine(TargetDir.FullName, "generatedsitemap.xml"));

				ITreeWalker walker = new BreadthFirstWalker();

				walker.Walk(root, new Act(AssignNavigationDocToFolders));
				walker.Walk(root, new Act(CreateBreadCrumb));
				walker.Walk(root, new Act(FixRelativePaths));
				walker.Walk(root, new Act(CreateHtml));

				foreach(FileToCopy file2Copy in staticFilesToCopy)
				{
					String dir = Path.GetDirectoryName(file2Copy.TargetFile);

					EnsureDirExists(dir);

					String targetFile = targetDir.FullName + "/" + file2Copy.TargetFile;

					if (File.Exists(targetFile))
					{
						if (File.GetLastWriteTime(targetFile) >= File.GetLastWriteTime(file2Copy.SourceFile))
						{
							continue;
						}

						File.Delete(targetFile);
					}

					File.Copy(file2Copy.SourceFile, targetFile);
				}
			}
			catch(Exception ex)
			{
				Console.WriteLine(ex);
				Console.Read();
			}
		}