Skip to content
Permalink
Browse files

Code formatting

  • Loading branch information...
Michal Altair Valášek
Michal Altair Valášek committed Mar 17, 2019
1 parent 9095ae8 commit 9026786ac8666d483f2ccea2823003272390c691
@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage.Blob;

@@ -1,10 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace Altairis.Xml4web.AzureSync {
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Altairis.Xml4web.AzureSync {

@@ -2,9 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;

@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Microsoft.WindowsAzure.Storage;
@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage.Blob;
using Newtonsoft.Json;
@@ -1,10 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace Altairis.Xml4web.Compiler {
@@ -1,11 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Altairis.Xml4web.Compiler {
public static class FileSystemHelper {
@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Altairis.Xml4web.Compiler {
namespace Altairis.Xml4web.Compiler {
public static class Namespaces {
public const string X4W = "http://schemas.altairis.cz/XML4web/PageMetadata/";
public const string X4H = "http://schemas.altairis.cz/XML4web/XsltHelper/";
@@ -5,14 +5,13 @@
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
using HtmlAgilityPack;

namespace Altairis.Xml4web.Compiler {
class Program {
public const int ERRORLEVEL_SUCCESS = 0;
public const int ERRORLEVEL_FAILURE = 1;

private static BuildConfiguration _config;
private static BuildConfiguration config;

static void Main(string[] args) {
var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
@@ -33,7 +32,7 @@ class Program {
PrepareFileSystem();

// Create site metadata document
var metadataFileName = Path.Combine(_config.WorkFolder, "metadata.xml");
var metadataFileName = Path.Combine(config.WorkFolder, "metadata.xml");
var metadataDocument = CreateMetadataDocument();
metadataDocument.Save(metadataFileName);

@@ -42,7 +41,7 @@ class Program {

// Check if there are some errors
tsw.Stop();
var logFiles = Directory.GetFiles(_config.WorkFolder, "*.log");
var logFiles = Directory.GetFiles(config.WorkFolder, "*.log");
if (!logFiles.Any()) {
Console.WriteLine($"Build completed successfully in {tsw.ElapsedMilliseconds} ms.");
Environment.Exit(ERRORLEVEL_SUCCESS);
@@ -70,7 +69,7 @@ class Program {
// Load configuration
Console.Write("Loading configuration...");
try {
_config = BuildConfiguration.Load(buildScriptFileName);
config = BuildConfiguration.Load(buildScriptFileName);
Console.WriteLine("OK");
}
catch (Exception ex) {
@@ -82,18 +81,18 @@ class Program {

private static void PrepareFileSystem() {
// Delete target and work folder
FileSystemHelper.DirectoryDelete(_config.TargetFolder);
Directory.CreateDirectory(_config.TargetFolder);
FileSystemHelper.DirectoryDelete(_config.WorkFolder);
Directory.CreateDirectory(_config.WorkFolder);
FileSystemHelper.DirectoryDelete(config.TargetFolder);
Directory.CreateDirectory(config.TargetFolder);
FileSystemHelper.DirectoryDelete(config.WorkFolder);
Directory.CreateDirectory(config.WorkFolder);

// Copy static data to output folder
if (Directory.Exists(_config.StaticFolder)) {
Console.Write($"Copying {_config.StaticFolder} to {_config.TargetFolder}...");
if (Directory.Exists(config.StaticFolder)) {
Console.Write($"Copying {config.StaticFolder} to {config.TargetFolder}...");
try {
var sw = new Stopwatch();
sw.Start();
FileSystemHelper.DirectoryCopy(_config.StaticFolder, _config.TargetFolder);
FileSystemHelper.DirectoryCopy(config.StaticFolder, config.TargetFolder);
sw.Stop();
Console.WriteLine($"OK in {sw.ElapsedMilliseconds} ms");
}
@@ -109,12 +108,12 @@ class Program {
Console.Write("Creating metadata document...");
var sw = new Stopwatch();
sw.Start();
var doc = SiteMetadataDocument.CreateFromFolder(_config.SourceFolder);
var doc = SiteMetadataDocument.CreateFromFolder(config.SourceFolder);
sw.Stop();

if (doc.Errors.Any()) {
Console.WriteLine($"Done in {sw.ElapsedMilliseconds} ms with {doc.Errors.Count()} errors, see metadata.xml.log for details.");
File.WriteAllLines(Path.Combine(_config.WorkFolder, "metadata.xml.log"), doc.Errors.Select(x => string.Join("\t", x.Key, x.Value)));
File.WriteAllLines(Path.Combine(config.WorkFolder, "metadata.xml.log"), doc.Errors.Select(x => string.Join("\t", x.Key, x.Value)));
}
else {
Console.WriteLine($"OK in {sw.ElapsedMilliseconds} ms");
@@ -124,23 +123,23 @@ class Program {

private static void RunAllTransforms(SiteMetadataDocument metadataDocument) {
Console.WriteLine("Running HTML transformations:");
foreach (var transform in _config.HtmlTransforms) {
var templateFileName = Path.Combine(_config.XsltFolder, transform.Key);
var outputFileName = Path.Combine(_config.WorkFolder, Path.GetFileNameWithoutExtension(transform.Key) + ".xml");
foreach (var transform in config.HtmlTransforms) {
var templateFileName = Path.Combine(config.XsltFolder, transform.Key);
var outputFileName = Path.Combine(config.WorkFolder, Path.GetFileNameWithoutExtension(transform.Key) + ".xml");

RunTransform(metadataDocument, templateFileName, outputFileName);

Console.Write(" Running post-processor...");
var proc = new XmlOutputProcessor(outputFileName, _config.TargetFolder, _config.PrependHtmlDoctype);
var proc = new XmlOutputProcessor(outputFileName, config.TargetFolder, config.PrependHtmlDoctype);
proc.SaveAllFiles(transform.Value);
Console.WriteLine("OK");
}

// Run raw transforms
Console.WriteLine("Running raw transformations:");
foreach (var transform in _config.RawTransforms) {
var templateFileName = Path.Combine(_config.XsltFolder, transform.Key);
var outputFileName = Path.Combine(_config.TargetFolder, transform.Value);
foreach (var transform in config.RawTransforms) {
var templateFileName = Path.Combine(config.XsltFolder, transform.Key);
var outputFileName = Path.Combine(config.TargetFolder, transform.Value);

RunTransform(metadataDocument, templateFileName, outputFileName);
}
@@ -157,8 +156,8 @@ class Program {

// Prepare transformation
var args = new XsltArgumentList();
args.AddExtensionObject(Namespaces.X4H, new XsltHelper(_config));
foreach (var item in _config.TransformParameters) {
args.AddExtensionObject(Namespaces.X4H, new XsltHelper(config));
foreach (var item in config.TransformParameters) {
args.AddParam(item.Key, Namespaces.X4C, item.Value);
}

@@ -175,7 +174,7 @@ class Program {
}
catch (Exception ex) {
Console.WriteLine("Failed!");
var errorLogName = Path.Combine(_config.WorkFolder, Path.GetFileName(templateFileName) + ".log");
var errorLogName = Path.Combine(config.WorkFolder, Path.GetFileName(templateFileName) + ".log");
Console.WriteLine($"For details see {errorLogName}");
File.WriteAllText(errorLogName, ex.ToString());
}
@@ -1,17 +1,11 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Schema;

namespace Altairis.Xml4web.Compiler {
public class SiteMetadataDocument : XmlDocument {
XmlNamespaceManager _nsmgr;
XmlNamespaceManager nsmgr;

public ICollection<KeyValuePair<string, string>> Errors { get; }
public string SourceFolderName { get; private set; }
@@ -31,18 +25,18 @@ public class SiteMetadataDocument : XmlDocument {
this.AppendChild(this.CreateElement("siteMetadata"));

// Add default namespaces
_nsmgr = new XmlNamespaceManager(this.NameTable);
_nsmgr.AddNamespace("dcterms", Namespaces.DCTerms);
_nsmgr.AddNamespace("dc", Namespaces.DC);
_nsmgr.AddNamespace("x4w", Namespaces.X4W);
_nsmgr.AddNamespace("x4h", Namespaces.X4H);
this.nsmgr = new XmlNamespaceManager(this.NameTable);
this.nsmgr.AddNamespace("dcterms", Namespaces.DCTerms);
this.nsmgr.AddNamespace("dc", Namespaces.DC);
this.nsmgr.AddNamespace("x4w", Namespaces.X4W);
this.nsmgr.AddNamespace("x4h", Namespaces.X4H);

// Add namespaces from file
var lines = File.ReadAllLines(namespaceFile);
foreach (var line in lines) {
var data = line.Split(new char[] { ':' }, 2);
if (data.Length != 2) continue;
_nsmgr.AddNamespace(data[0], data[1]);
this.nsmgr.AddNamespace(data[0], data[1]);
this.DocumentElement.SetAttribute("xmlns:" + data[0], data[1]);
}

@@ -63,7 +57,7 @@ public class SiteMetadataDocument : XmlDocument {
// Import metadata from index page
var indexFileName = Path.Combine(folderName, "index.md");
if (File.Exists(indexFileName)) {
foreach (var item in GetMetadataElementsFromFile(indexFileName)) {
foreach (var item in this.GetMetadataElementsFromFile(indexFileName)) {
folderElement.AppendChild(item);
}
}
@@ -72,7 +66,7 @@ public class SiteMetadataDocument : XmlDocument {
foreach (var fileName in Directory.GetFiles(folderName, "*.md")) {
var fileElement = this.CreateElement("file");
fileElement.SetAttribute("path", pathId + "/" + Path.GetFileNameWithoutExtension(fileName));
foreach (var item in GetMetadataElementsFromFile(fileName)) {
foreach (var item in this.GetMetadataElementsFromFile(fileName)) {
fileElement.AppendChild(item);
}
folderElement.AppendChild(fileElement);
@@ -83,7 +77,7 @@ public class SiteMetadataDocument : XmlDocument {

// Recurse folders
foreach (var item in Directory.GetDirectories(folderName)) {
ScanFolder(item, folderElement);
this.ScanFolder(item, folderElement);
}

}
@@ -95,7 +89,7 @@ public class SiteMetadataDocument : XmlDocument {
if (separatorIndex == -1 || separatorIndex == 0 || separatorIndex == item.Key.Length - 1) {
this.Errors.Add(new KeyValuePair<string, string>(mdFileName, $"Invalid syntax of metadata key \"{item.Key}\"."));
}
else if (string.IsNullOrEmpty(_nsmgr.LookupNamespace(item.Key.Substring(0, separatorIndex)))) {
else if (string.IsNullOrEmpty(this.nsmgr.LookupNamespace(item.Key.Substring(0, separatorIndex)))) {
this.Errors.Add(new KeyValuePair<string, string>(mdFileName, $"Unknown prefix of metadata key \"{item.Key}\"."));
}
else {
@@ -131,7 +125,7 @@ public class SiteMetadataDocument : XmlDocument {
var qnData = qualifiedName.Split(':');
if (qnData.Length != 2) throw new ArgumentException($"Value '{qualifiedName}' must contain exactly one ':'.", nameof(qualifiedName));

return this.CreateElement(qualifiedName, _nsmgr.LookupNamespace(qnData[0]));
return this.CreateElement(qualifiedName, this.nsmgr.LookupNamespace(qnData[0]));
}

private XmlElement CreateQualifiedElement(string qualifiedName, string text) {
@@ -1,37 +1,34 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace Altairis.Xml4web.Compiler {
public class XmlOutputProcessor {
readonly XmlDocument _mainDoc;
readonly string _baseFolder;
readonly string _prependDoctype;
readonly XmlNamespaceManager _nsmgr;
readonly XmlDocument mainDoc;
readonly string baseFolder;
readonly string prependDoctype;
readonly XmlNamespaceManager nsmgr;

public XmlOutputProcessor(string mainDocName, string baseFolder, string prependDoctype) {
_mainDoc = new XmlDocument();
_mainDoc.Load(mainDocName);
this.mainDoc = new XmlDocument();
this.mainDoc.Load(mainDocName);

_nsmgr = new XmlNamespaceManager(_mainDoc.NameTable);
_nsmgr.AddNamespace("x4o", Namespaces.X4O);
this.nsmgr = new XmlNamespaceManager(this.mainDoc.NameTable);
this.nsmgr.AddNamespace("x4o", Namespaces.X4O);

_baseFolder = baseFolder;
_prependDoctype = prependDoctype;
this.baseFolder = baseFolder;
this.prependDoctype = prependDoctype;
}

public void SaveAllFiles(string mainFileName) {
// Check if there are multiple output documents
var outputDocuments = _mainDoc.SelectNodes("/x4o:root/x4o:document", this._nsmgr);
var outputDocuments = this.mainDoc.SelectNodes("/x4o:root/x4o:document", this.nsmgr);

if (outputDocuments.Count == 0) {
// Single document
this.SaveFile(_mainDoc, mainFileName);
this.SaveFile(this.mainDoc, mainFileName);
}
else {
// Multiple documents
@@ -50,15 +47,15 @@ public class XmlOutputProcessor {

var replacements = new NameValueCollection();

foreach (XmlElement item in doc.SelectNodes("//*[@x4o:unescape='true']", _nsmgr)) {
foreach (XmlElement item in doc.SelectNodes("//*[@x4o:unescape='true']", this.nsmgr)) {
item.RemoveAttribute("unescape", Namespaces.X4O);
var key = $"<!--REPLACE:{Guid.NewGuid()}-->";
replacements.Add(key, item.InnerText);
item.InnerXml = key;
}

var sb = new StringBuilder();
if (!string.IsNullOrEmpty(_prependDoctype) && doc.DocumentElement.LocalName.Equals("html", StringComparison.OrdinalIgnoreCase)) sb.AppendLine(_prependDoctype);
if (!string.IsNullOrEmpty(this.prependDoctype) && doc.DocumentElement.LocalName.Equals("html", StringComparison.OrdinalIgnoreCase)) sb.AppendLine(this.prependDoctype);
var settings = new XmlWriterSettings {
Encoding = Encoding.UTF8,
Indent = true,
@@ -74,7 +71,7 @@ public class XmlOutputProcessor {
sb = sb.Replace(key, replacements[key]);
}

fileName = Path.Combine(_baseFolder, fileName.Trim('/', '\\'));
fileName = Path.Combine(this.baseFolder, fileName.Trim('/', '\\'));
Directory.CreateDirectory(Path.GetDirectoryName(fileName));
File.WriteAllText(fileName, sb.ToString());
}
Oops, something went wrong.

0 comments on commit 9026786

Please sign in to comment.
You can’t perform that action at this time.