// // Authors // Sebastien Pouliot // // Copyright 2013 Xamarin Inc. http://www.xamarin.com // Copyright 2018 Microsoft Inc. // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the // "Software"), to deal in the Software without restriction, including // without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to // permit persons to whom the Software is furnished to do so, subject to // the following conditions: // // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // using System; using System.IO; using System.Collections.Generic; using System.Linq; using System.Xml.Linq; using System.Text; namespace Xamarin.ApiDiff { public class HtmlFormatter : Formatter { public override string LesserThan => "<"; public override string GreaterThan => ">"; protected void Indent (TextWriter output) { for (int i = 0; i < State.Indent; i++) output.Write ("\t"); } public override void BeginDocument (TextWriter output, string title) { output.WriteLine ("
"); if (State.Colorize) { output.WriteLine (""); } output.WriteLine ( @""); } public override void BeginAssembly (TextWriter output) { output.WriteLine ($"

{State.Assembly}.dll

"); if (!State.IgnoreNonbreaking) { output.WriteLine ("Hide non-breaking changes"); output.WriteLine (""); output.WriteLine ("
"); } output.WriteLine ("
"); } public override void EndAssembly (TextWriter output) { output.WriteLine ("
"); output.WriteLine ("
"); } public override void BeginNamespace (TextWriter output, string action) { output.WriteLine ($"
"); output.WriteLine ($"

{action}Namespace {State.Namespace}

"); } public override void EndNamespace (TextWriter output) { output.WriteLine ($"
"); } public override void BeginTypeAddition (TextWriter output) { output.WriteLine ($"
"); output.WriteLine ($"

New Type {State.Namespace}.{State.Type}

"); output.WriteLine ("
");
		}

		public override void EndTypeAddition (TextWriter output)
		{
			output.WriteLine ("
"); output.WriteLine ($"
"); } public override void BeginTypeModification (TextWriter output) { output.WriteLine ($"
"); output.WriteLine ($"

Type Changed: {State.Namespace}.{State.Type}

"); } public override void EndTypeModification (TextWriter output) { output.WriteLine ($"
"); } public override void BeginTypeRemoval (TextWriter output) { output.Write ($"

Removed Type {State.Namespace}.{State.Type}

"); } public override void BeginMemberAddition (TextWriter output, IEnumerable list, MemberComparer member) { output.WriteLine ("
"); if (State.BaseType == "System.Enum") { output.WriteLine ("

Added value{0}:

", list.Count () > 1 ? "s" : String.Empty); output.WriteLine ("
");
			} else {
				output.WriteLine ("

Added {0}:

", list.Count () > 1 ? member.GroupName : member.ElementName); output.WriteLine ("
");
			}
			State.Indent++;
		}

		public override void AddMember (TextWriter output, MemberComparer member, bool isInterfaceBreakingChange, string obsolete, string description)
		{
			output.Write ("", member.ElementName, isInterfaceBreakingChange ? "breaking" : string.Empty, isInterfaceBreakingChange ? "data-is-breaking" : "data-is-non-breaking");
			output.Write ($"{obsolete}{description}");
			output.WriteLine ("");
		}

		public override void EndMemberAddition (TextWriter output)
		{
			State.Indent--;
			output.WriteLine ("
"); output.WriteLine ("
"); } public override void BeginMemberModification (TextWriter output, string sectionName) { output.WriteLine ($"

{sectionName}:

"); output.WriteLine ("
");
		}

		public override void EndMemberModification (TextWriter output)
		{
			output.WriteLine ("
"); } public override void BeginMemberRemoval (TextWriter output, IEnumerable list, MemberComparer member) { if (State.BaseType == "System.Enum") { output.WriteLine ("

Removed value{0}:

", list.Count () > 1 ? "s" : String.Empty); output.WriteLine ("
");
			} else {
				output.WriteLine ("

Removed {0}:

\n", list.Count () > 1 ? member.GroupName : member.ElementName); output.WriteLine ("
");
			}
			State.Indent++;
		}

		public override void RemoveMember (TextWriter output, MemberComparer member, bool breaking, string obsolete, string description)
		{
			Indent (output);
			output.Write ("", member.ElementName, breaking ? "data-is-breaking" : "data-is-non-breaking", breaking ? "breaking" : string.Empty);
			if (obsolete.Length > 0) {
				output.Write (obsolete);
				Indent (output);
			}
			output.Write (description);
			output.WriteLine ("");
		}

		public override void RenderObsoleteMessage (StringBuilder output, MemberComparer member, string description, string optionalObsoleteMessage)
		{
			output.Append ($"");
			output.Append ("[Obsolete (");
			if (!String.IsNullOrEmpty (optionalObsoleteMessage))
				output.Append ('"').Append (optionalObsoleteMessage).Append ('"');
			output.AppendLine (")]");
			output.Append (description);
			output.Append ("");
		}

		public override void EndMemberRemoval (TextWriter output)
		{
			State.Indent--;
			output.WriteLine ("
");; } public override void DiffAddition (StringBuilder output, string text, bool breaking) { output.Append (""); output.Append (text); output.Append (""); } public override void DiffModification (StringBuilder output, string old, string @new, bool breaking) { if (old.Length > 0) DiffRemoval (output, old, breaking); if (old.Length > 0 && @new.Length > 0) output.Append (' '); if (@new.Length > 0) DiffAddition (output, @new, false); } public override void DiffRemoval (StringBuilder output, string text, bool breaking) { output.Append (""); output.Append (text); output.Append (""); } public override void Diff (TextWriter output, ApiChange apichange) { output.Write ("
", apichange.Breaking ? "data-is-breaking" : "data-is-non-breaking"); foreach (var line in apichange.Member.ToString ().Split ('\n')) { output.Write ('\t'); output.WriteLine (line); } output.Write ("
"); } } }