Filkolev

Semantic HTML

May 27th, 2015
69
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Text.RegularExpressions;
  3.  
  4. public class SemanticHtml
  5. {
  6.     public static void Main()
  7.     {
  8.         const string openingTagPattern = @"<\s*div\s+(?<first>.*?)(?:id|class)\s*=\s*""(?<tagName>main|header|nav|article|section|aside|footer)""(?<second>.*?)\s*>";
  9.         const string closingTagPattern = @"<\/div>\s*<!--\s*(?<tagName>main|header|nav|article|section|aside|footer)\s*-->";
  10.  
  11.         string input = Console.ReadLine();
  12.  
  13.         while (input != "END")
  14.         {
  15.             if (Regex.IsMatch(input, openingTagPattern))
  16.             {
  17.                 var match = Regex.Match(input, openingTagPattern);
  18.  
  19.                 string first = match.Groups["first"].ToString();
  20.                 string second = match.Groups["second"].ToString();
  21.                 string tagName = match.Groups["tagName"].ToString();
  22.  
  23.                 string tag = string.Format("{0} {1} {2}", tagName, first, second);
  24.                 tag = Regex.Replace(tag, @"\s+", " ").Trim();
  25.                 tag = "<" + tag + ">";
  26.  
  27.                 Console.WriteLine(tag);
  28.             }
  29.             else if (Regex.IsMatch(input, closingTagPattern))
  30.             {
  31.                 var match = Regex.Match(input, closingTagPattern);
  32.  
  33.                 string tagName = match.Groups["tagName"].ToString();
  34.  
  35.                 string tag = string.Format("</{0}>", tagName);
  36.  
  37.                 Console.WriteLine(tag);
  38.             }
  39.             else
  40.             {
  41.                 Console.WriteLine(input);
  42.             }
  43.  
  44.             input = Console.ReadLine();
  45.         }
  46.     }
  47. }
RAW Paste Data