diff api/Exporters/WarFoundryHtmlExporter.cs @ 262:900adf96a915

Closes #281: Export multiple stat lines in HTML * Rebuild HTML generation to take advantage of existing columns where stat type matches table stat type * Tidy up the styling
author IBBoard <dev@ibboard.co.uk>
date Mon, 31 May 2010 15:33:41 +0000
parents b9b8b0e60c31
children 08a9c960e17f
line wrap: on
line diff
--- a/api/Exporters/WarFoundryHtmlExporter.cs	Sat May 29 20:03:17 2010 +0000
+++ b/api/Exporters/WarFoundryHtmlExporter.cs	Mon May 31 15:33:41 2010 +0000
@@ -57,8 +57,9 @@
 			metaCharset.SetAttribute("content", "text/html;charset=UTF-8");
 			head.AppendChild(metaCharset);
 			XmlElement style = doc.CreateElement("style");
-			style.InnerText = "table, th, td { border: 1px solid #000; border-spacing: 0 }" +
-				"table table { width: 100% }";
+			style.InnerText = "able, th, td { border: 1px solid #000; border-spacing: 0; border-collapse: collapse; margin: 0 }\n"
+				+"table table { width: 100%; border-width: 0; margin: -2px }\n"
+				+"table table td { border-width:0 1px }";
 			head.AppendChild(style);
 			XmlElement body = doc.CreateElement("body");
 			html.AppendChild(body);
@@ -102,7 +103,7 @@
 			
 			foreach (Unit unit in army.GetUnits())
 			{
-				tables[GetFirstStatType(unit)].AppendChild(CreateUnitRow(unit, doc));
+				CreateUnitRow(unit, tables[GetFirstStatType(unit)]);
 			}
 			
 			return DictionaryUtils.ToArray(tables);
@@ -150,16 +151,38 @@
 			return table;
 		}
 		
-		private XmlElement CreateUnitRow(Unit unit, XmlDocument doc)
+		private XmlElement CreateUnitRow(Unit unit, XmlElement tableElem)
 		{
+			XmlDocument doc = tableElem.OwnerDocument;
 			XmlElement row = doc.CreateElement("tr");
+			tableElem.AppendChild(row);
+			Stat[][] memberStats = unit.UnitStatsArraysWithName;
+			string[] statTypeIDs = unit.UnitStatsArrayIDs;
+			string defaultStatType = GetFirstStatType(statTypeIDs);
+			int statRowCount = 0;
+			bool hasOther = false;
+			
+			foreach (string statTypeID in statTypeIDs)
+			{
+				if (statTypeID.Equals(defaultStatType))
+				{
+					statRowCount++;
+				}
+				else if (!hasOther)
+				{
+					statRowCount++;
+					hasOther = true;
+				}
+			}
+			
 			XmlElement name = doc.CreateElement("td");
 			name.InnerText = unit.Name;
+			SetRowSpan(name, statRowCount);
 			row.AppendChild(name);
-			CreateStatsBlock(unit, row);
+			CreateStatsBlock(row, memberStats, statTypeIDs);
 
 			StringBuilder sb = new StringBuilder();
-			UnitEquipmentItem[] unitEquipment = unit.GetEquipment ();
+			UnitEquipmentItem[] unitEquipment = unit.GetEquipment();
 			
 			if (unitEquipment.Length > 0)
 			{
@@ -189,7 +212,7 @@
 						else
 						{
 							int number = UnitEquipmentUtil.GetEquipmentAmountTaken(unit, equip);
-							amountString = GetEquipmentAmountRatioTranslation (amount, number);
+							amountString = GetEquipmentAmountRatioTranslation(amount, number);
 						}
 					}
 					else
@@ -213,7 +236,7 @@
 			ICollection<Ability> abilities = unit.Abilities;
 			
 			if (abilities.Count > 0)
-			{				
+			{
 				bool addSeparator = false;
 				
 				foreach (Ability ability in abilities)
@@ -232,66 +255,110 @@
 				
 				sb.Append(". ");
 			}
-						
+			
 			XmlElement notes = doc.CreateElement("td");
 			notes.InnerText = sb.ToString();
+			SetRowSpan(notes, statRowCount);
 			row.AppendChild(notes);
 			
 			XmlElement points = doc.CreateElement("td");
 			points.InnerText = unit.Points.ToString();
+			SetRowSpan(points, statRowCount);
 			row.AppendChild(points);
 			
 			return row;
 		}
 		
-		private void CreateStatsBlock(Unit unit, XmlElement unitRow)
+		private static void SetRowSpan(XmlElement xmlElement, int statRowCount)
+		{
+			if (statRowCount > 1)
+			{
+				xmlElement.SetAttribute("rowspan", statRowCount.ToString());
+			}
+		}
+		
+		private void CreateStatsBlock(XmlElement unitRow, Stat[][] memberStats, string[] statTypeIDs)
 		{
 			XmlDocument doc = unitRow.OwnerDocument;
-			XmlElement statsWrapperCell = doc.CreateElement("td");
-			unitRow.AppendChild(statsWrapperCell);
-			Stat[][] memberStats = unit.UnitStatsArraysWithName;
-			statsWrapperCell.SetAttribute("colspan", memberStats[0].Length.ToString());
-			string[] statTypeIDs = unit.UnitStatsArrayIDs;
 			string defaultStatType = GetFirstStatType(statTypeIDs);
-			Dictionary<string, XmlElement> statTables = CreateEmptyStatsTables(statTypeIDs, statsWrapperCell);
+			
+			Stat[] defaultStatLine = memberStats[0];
+			int defaultStatLineCount = defaultStatLine.Length;
+			AddStatCell(defaultStatLine[0].SlotValueString, unitRow);
+			
+			for (int i = 1; i < defaultStatLineCount; i++)
+			{
+				string statText = GetDefaultStatCellText(defaultStatLine[i]);
+				AddStatCell(statText, unitRow);
+			}
 			
 			int statCount = statTypeIDs.Length;
 			
-			for (int i = 0; i < statCount; i++)
+			if (statCount > 1)
 			{
-				Stat[] statLine = memberStats[i];
-				string statTypeID = statTypeIDs[i];
-				XmlElement tableElement = DictionaryUtils.GetValue(statTables, statTypeID);
-				int statLineCount = statLine.Length;
-				XmlElement statRow = doc.CreateElement("tr");
-				tableElement.AppendChild(statRow);
-				GetStatCellTextDelegate statCellTextDelegate = (statTypeID.Equals(defaultStatType) ? new GetStatCellTextDelegate(GetDefaultStatCellText) : new GetStatCellTextDelegate(GetOtherStatCellText));
-				AddStatCell(statLine[0].SlotValueString, statRow);
+				XmlElement unitTable = (XmlElement)unitRow.ParentNode;
+				Dictionary<string, XmlElement> statParents = CreateStatsParentElements(statTypeIDs, unitTable);
+				
+				for (int i = 1; i < statCount; i++)
+				{
+					Stat[] statLine = memberStats[i];
+					string statTypeID = statTypeIDs[i];
+					XmlElement tableElement = DictionaryUtils.GetValue(statParents, statTypeID);
+					int statLineCount = statLine.Length;
+					XmlElement statRow = doc.CreateElement("tr");
+					tableElement.AppendChild(statRow);
+					GetStatCellTextDelegate statCellTextDelegate = (statTypeID.Equals(defaultStatType) ? new GetStatCellTextDelegate(GetDefaultStatCellText) : new GetStatCellTextDelegate(GetOtherStatCellText));
+					AddStatCell(statLine[0].SlotValueString, statRow);
+				
+					for (int j = 1; j < statLineCount; j++)
+					{
+						string statText = statCellTextDelegate(statLine[j]);
+						AddStatCell(statText, statRow);
+					}
+				}
 				
-				for (int j = 1; j < statLineCount; j++)
+				if (statParents.Count > 1)
 				{
-					string statText = statCellTextDelegate(statLine[j]);
-					AddStatCell(statText, statRow);
+					AddOtherUnitStatTables(statParents, unitTable, defaultStatLineCount);
+				}
+			}
+		}
+		
+		private static void AddOtherUnitStatTables(Dictionary<string, XmlElement> statParents, XmlElement unitTable, int defaultStatLineCount)
+		{
+			XmlDocument doc = unitTable.OwnerDocument;
+			XmlElement otherStatsRow = doc.CreateElement("tr");
+			unitTable.AppendChild(otherStatsRow);
+			XmlElement otherStatsCell = doc.CreateElement("td");
+			otherStatsCell.SetAttribute("colspan", defaultStatLineCount.ToString());
+			otherStatsRow.AppendChild(otherStatsCell);
+			
+			foreach (XmlElement tableElem in statParents.Values)
+			{
+				if (tableElem != unitTable)
+				{
+					otherStatsCell.AppendChild(tableElem);
 				}
 			}
 		}
 
-		private Dictionary<string, XmlElement> CreateEmptyStatsTables(string[] statTypeIDs, XmlElement containerCell)
+		private Dictionary<string, XmlElement> CreateStatsParentElements(string[] statTypeIDs, XmlElement parentTable)
 		{
-			Dictionary<string, XmlElement> statTables = new Dictionary<string, XmlElement>();
-			XmlDocument doc = containerCell.OwnerDocument;
+			Dictionary<string, XmlElement> statParents = new Dictionary<string, XmlElement>();
+			XmlDocument doc = parentTable.OwnerDocument;
+			string defaultStatTypeID = GetFirstStatType(statTypeIDs);
+			statParents[defaultStatTypeID] = parentTable;
 			
 			foreach (string statTypeID in statTypeIDs)
 			{
-				if (!statTables.ContainsKey(statTypeID))
+				if (!statParents.ContainsKey(statTypeID))
 				{
 					XmlElement tableElement = doc.CreateElement("table");
-					containerCell.AppendChild(tableElement);
-					statTables[statTypeID] = tableElement;
+					statParents[statTypeID] = tableElement;
 				}
 			}
 			
-			return statTables;
+			return statParents;
 		}
 
 		private string GetDefaultStatCellText(Stat stat)