Advertisement
dimipan80

Semantic HTML

May 14th, 2015
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.47 KB | None | 0 0
  1. /* You are given an HTML code, written in the old non-semantic style using tags like <div id="header">, <div class="section">, etc. Your task is to write a program that converts this HTML to semantic HTML by changing tags like <div id="header"> to their semantic equivalent like <header>.
  2.  * The non-semantic tags that should be converted are always <div>s and have either id or class with one of the following values: "main", "header", "nav", "article", "section", "aside" or "footer". Their corresponding closing tags are always followed by a comment like <!-- header -->, <!-- nav -->, etc. staying at the same line, after the tag.
  3.  * In all converted tags you should replace multiple spaces (like <header      style="color:red">) with a single space and remove excessive spaces at the end (like <footer      >). */
  4.  
  5. namespace Semantic_HTML
  6. {
  7.     using System;
  8.     using System.Collections.Generic;
  9.     using System.Linq;
  10.     using System.Text;
  11.     using System.Text.RegularExpressions;
  12.  
  13.     class SemanticHTML
  14.     {
  15.         private const string OpenTagsPattern =
  16. @"<(div)\s.*?((?:id|class)\s*=\s*""([a-z]+)"").*?>";
  17.  
  18.         private const string CloseTagsPattern = @"<\/div>\s+<!--\s*([a-z]+)\s*-->";
  19.         private static HashSet<string> semanticTags;
  20.  
  21.         static void Main()
  22.         {
  23.             semanticTags = new HashSet<string>() { "main", "header", "nav", "article", "section", "aside", "footer" };
  24.  
  25.             string inputLine = Console.ReadLine();
  26.             while (inputLine != "END")
  27.             {
  28.                 if (Regex.IsMatch(inputLine, OpenTagsPattern))
  29.                 {
  30.                     ReplaceOpenTags(inputLine);
  31.                 }
  32.                 else if (Regex.IsMatch(inputLine, CloseTagsPattern))
  33.                 {
  34.                     ReplaceCloseTags(inputLine);
  35.                 }
  36.                 else
  37.                 {
  38.                     Console.WriteLine(inputLine);
  39.                 }
  40.  
  41.                 inputLine = Console.ReadLine();
  42.             }
  43.         }
  44.  
  45.         private static void ReplaceOpenTags(string htmlLine)
  46.         {
  47.             MatchCollection openTags = Regex.Matches(htmlLine, OpenTagsPattern);
  48.             foreach (Match openTag in openTags)
  49.             {
  50.                 string semanticTag = openTag.Groups[3].Value;
  51.                 if (!semanticTags.Contains(semanticTag))
  52.                 {
  53.                     Console.WriteLine(htmlLine);
  54.                 }
  55.                 else
  56.                 {
  57.                     string divTag = openTag.Groups[1].Value;
  58.                     string attribute = openTag.Groups[2].Value;
  59.  
  60.                     StringBuilder sb = new StringBuilder(openTag.Value);
  61.                     sb.Replace(divTag, semanticTag).Replace(attribute, string.Empty);
  62.                     string result = Regex.Replace(sb.ToString(), @"\s+>", ">");
  63.                     result = Regex.Replace(result, @"\s{2,}", " ");
  64.  
  65.                     Console.WriteLine(htmlLine.Replace(openTag.Value, result));
  66.                 }
  67.             }
  68.         }
  69.  
  70.         private static void ReplaceCloseTags(string htmlLine)
  71.         {
  72.             MatchCollection closeTags = Regex.Matches(htmlLine, CloseTagsPattern);
  73.  
  74.             htmlLine = closeTags.Cast<Match>().Where(closeTag => semanticTags.Contains(closeTag.Groups[1].Value)).Aggregate(htmlLine, (current, closeTag) => current.Replace(closeTag.Value, string.Format("</{0}>", closeTag.Groups[1].Value)));
  75.  
  76.             Console.WriteLine(htmlLine);
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement