Advertisement
ivandrofly

Parsing subtitle using AngleSharp/State tecni

May 26th, 2024
483
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.08 KB | None | 0 0
  1. // Parsing JacoSub (subtitle edit)
  2.  
  3. private struct ParseData
  4.         {
  5.             public TimeSpan StartTime { get; set; }
  6.             public TimeSpan EndTime { get; set; }
  7.         }
  8.  
  9.         Paragraph Paragraph(string line)
  10.         {
  11.             return StartTime(line, 0);
  12.         }
  13.  
  14.         Paragraph StartTime(string line, int position)
  15.         {
  16.             var parseData = new ParseData();
  17.             // H:MM:SS.FF H:MM:SS.FF directive {comment} text {comment} more text...
  18.             if (position + 10 < line.Length)
  19.             {
  20.                 var timestamp = line.Substring(position, 10);
  21.                 parseData.StartTime = TimeSpan.ParseExact(timestamp, "H:MM:SS.FF", CultureInfo.CurrentCulture);
  22.                 return EndTime(line, position + 10, ref parseData); // read the time cod from 10 index
  23.             }
  24.  
  25.             return null;
  26.         }
  27.  
  28.         Paragraph EndTime(string line, int position, ref ParseData parseData)
  29.         {
  30.             // H:MM:SS.FF H:MM:SS.FF directive {comment} text {comment} more text...
  31.             while (position < line.Length && line[position] == ' ') position++;
  32.  
  33.             if (position + 10 < line.Length)
  34.             {
  35.                 var timestamp = line.Substring(position, 10);
  36.                 parseData.EndTime = TimeSpan.ParseExact(timestamp, "H:MM:SS.FF", CultureInfo.CurrentCulture);
  37.                 return EndTime(line, position + 10, ref parseData); // read the time cod from 10 index
  38.             }
  39.  
  40.             return null;
  41.         }
  42.  
  43.         Paragraph Text(string line, int position, ref ParseData token)
  44.         {
  45.             // H:MM:SS.FF H:MM:SS.FF directive {comment} text {comment} more text...
  46.             if (position + 1 < line.Length)
  47.             {
  48.                 return new Paragraph()
  49.                 {
  50.                     StartTime = new TimeCode(token.StartTime.TotalMilliseconds),
  51.                     EndTime = new TimeCode(token.EndTime.TotalMilliseconds),
  52.                     Text = line.Substring(position).TrimStart()
  53.                 };
  54.             }
  55.  
  56.             return null;
  57.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement