コード例 #1
0
        /// <summary>Parses the specified text, using the specified format.</summary>
        /// <remarks>The created <see cref="SubtitleCollection" /> will have its <see cref="SubtitleProperties" /> property set to null.
        /// It is mandatory to use <see cref="SubtitleCollection.SetPropertiesForAll" /> after.</remarks>
        internal ParsingProperties Parse(string text, SubtitleFormat format, float inputFrameRate,
                                         out SubtitleCollection collection, out IncompleteSubtitleCollection incompleteSubtitles)
        {
            collection          = new SubtitleCollection();
            incompleteSubtitles = new IncompleteSubtitleCollection();
            ParsingProperties properties = new ParsingProperties();

            properties.InputFrameRate = inputFrameRate;

            Regex subtitleRegex = null;
            int   bodyIndex     = 0;

            text = ClearComments(text, format);

            /* Read the headers if available */
            if (format.Mode == SubtitleMode.Both)
            {
                //Read headers to know if format is using Times or Frames
                bodyIndex = text.Length;
                int lastIndex = ReadHeaders(text, bodyIndex, format, properties);
                subtitleRegex = CreateSubtitleRegex(format, properties.TimingMode);

                /* Detect body index from matching the first subtitle or the end of headers */
                bodyIndex = FindBodyIndex(text, format, subtitleRegex);
                if (lastIndex > bodyIndex)
                {
                    bodyIndex = lastIndex;
                }
            }
            else
            {
                //End of headers is detected by start of subtitles' body
                properties.TimingMode = format.ModeAsTimingMode;
                subtitleRegex         = CreateSubtitleRegex(format);
                bodyIndex             = FindBodyIndex(text, format, subtitleRegex);
                ReadHeaders(text, bodyIndex, format, properties);
            }

            /* Get properties from the whole input, if available */
            format.GlobalInputGetProperties(text, properties);

            int textLength = text.Length;

            /* Read the subtitles */
            bodyIndex = ReadSubtitles(text, bodyIndex, textLength, subtitleRegex, format,
                                      properties, collection, incompleteSubtitles);

            /* Read the end text of the subtitles */
            bodyIndex = ReadBodyEnd(text, bodyIndex, format, collection, incompleteSubtitles);

            /* Check if there's still text remaining */
            if ((bodyIndex < textLength) && includeIncompleteSubtitles)
            {
                AddIncompleteSubtitle(incompleteSubtitles, text.Substring(bodyIndex), collection.Count);
            }

            return(properties);
        }
コード例 #2
0
 private void AddIncompleteSubtitle(IncompleteSubtitleCollection incompleteSubtitles, string incompleteText,
                                    int previousSubtitle)
 {
     if (!HasOnlyWhiteSpaces(incompleteText))
     {
         IncompleteSubtitle incompleteSubtitle = new IncompleteSubtitle(previousSubtitle, incompleteText);
         incompleteSubtitles.Add(incompleteSubtitle);
     }
 }
コード例 #3
0
 private void AddIncompleteSubtitleIfExists(string text, Match match, int bodyIndex,
                                            int subtitleCount, IncompleteSubtitleCollection incompleteSubtitles)
 {
     if (includeIncompleteSubtitles && IsThereIncompleteText(match, bodyIndex))
     {
         int    length         = match.Index - bodyIndex;
         string incompleteText = text.Substring(bodyIndex, length);
         AddIncompleteSubtitle(incompleteSubtitles, incompleteText, subtitleCount);
     }
 }
コード例 #4
0
        private int ReadBodyEnd(string text, int bodyIndex, SubtitleFormat format,
                                SubtitleCollection collection, IncompleteSubtitleCollection incompleteSubtitles)
        {
            Regex bodyEnd      = new Regex(format.BodyEndIn + @"\s*", RegexOptions.IgnoreCase);
            Match bodyEndMatch = bodyEnd.Match(text, bodyIndex);

            if (bodyEndMatch.Success)
            {
                AddIncompleteSubtitleIfExists(text, bodyEndMatch, bodyIndex, collection.Count, incompleteSubtitles);
                bodyIndex = bodyEndMatch.Index + bodyEndMatch.Length;
            }
            return(bodyIndex);
        }
コード例 #5
0
	/// <summary>Parses the specified text, using the specified format.</summary>
	/// <remarks>The created <see cref="SubtitleCollection" /> will have its <see cref="SubtitleProperties" /> property set to null.
	/// It is mandatory to use <see cref="SubtitleCollection.SetPropertiesForAll" /> after.</remarks>
	internal ParsingProperties Parse (string text, SubtitleFormat format, float inputFrameRate,
			out SubtitleCollection collection, out IncompleteSubtitleCollection incompleteSubtitles){

		collection = new SubtitleCollection();
		incompleteSubtitles = new IncompleteSubtitleCollection();
		ParsingProperties properties = new ParsingProperties();
		properties.InputFrameRate = inputFrameRate;

		Regex subtitleRegex = null;
		int bodyIndex = 0;

		text = ClearComments(text, format);

		/* Read the headers if available */
		if (format.Mode == SubtitleMode.Both) {
			//Read headers to know if format is using Times or Frames
			bodyIndex = text.Length;
			int lastIndex = ReadHeaders(text, bodyIndex, format, properties);
			subtitleRegex = CreateSubtitleRegex(format, properties.TimingMode);

			/* Detect body index from matching the first subtitle or the end of headers */
			bodyIndex = FindBodyIndex(text, format, subtitleRegex);
			if (lastIndex > bodyIndex)
				bodyIndex = lastIndex;
		}
		else {
			//End of headers is detected by start of subtitles' body
			properties.TimingMode = format.ModeAsTimingMode;
			subtitleRegex = CreateSubtitleRegex(format);
			bodyIndex = FindBodyIndex(text, format, subtitleRegex);
			ReadHeaders(text, bodyIndex, format, properties);
		}

		/* Get properties from the whole input, if available */
		format.GlobalInputGetProperties(text, properties);

		int textLength = text.Length;

		/* Read the subtitles */
		bodyIndex = ReadSubtitles(text, bodyIndex, textLength, subtitleRegex, format,
			properties, collection, incompleteSubtitles);

    	/* Read the end text of the subtitles */
    	bodyIndex = ReadBodyEnd(text, bodyIndex, format, collection, incompleteSubtitles);

		/* Check if there's still text remaining */
    	if ((bodyIndex < textLength) && includeIncompleteSubtitles)
    		AddIncompleteSubtitle(incompleteSubtitles, text.Substring(bodyIndex), collection.Count);

    	return properties;
	}
コード例 #6
0
        private int ReadSubtitles(string text, int bodyIndex, int textLength, Regex subtitleRegex, SubtitleFormat format,
                                  ParsingProperties properties, SubtitleCollection collection, IncompleteSubtitleCollection incompleteSubtitles)
        {
            Subtitle previousSubtitle = null;

            /* Read the subtitles. BodyIndex points to the start of the subtitles, skipping its possible beginning text*/
            while (bodyIndex < textLength)
            {
                Match match = subtitleRegex.Match(text, bodyIndex);
                if (match.Success)
                {
                    Subtitle subtitle = ParseSubtitle(match, format, properties, previousSubtitle);
                    collection.Add(subtitle);
                    AddIncompleteSubtitleIfExists(text, match, bodyIndex, collection.Count, incompleteSubtitles);
                    bodyIndex        = match.Index + match.Length;
                    previousSubtitle = subtitle;
                }
                else
                {
                    break;
                }
            }
            return(bodyIndex);
        }
コード例 #7
0
	private void AddIncompleteSubtitleIfExists (string text, Match match, int bodyIndex,
    		int subtitleCount, IncompleteSubtitleCollection incompleteSubtitles) {

    	if (includeIncompleteSubtitles && IsThereIncompleteText(match, bodyIndex)) {
    		int length = match.Index - bodyIndex;
    		string incompleteText = text.Substring(bodyIndex, length);
	    	AddIncompleteSubtitle(incompleteSubtitles, incompleteText, subtitleCount);
		}
    }
コード例 #8
0
	private void AddIncompleteSubtitle (IncompleteSubtitleCollection incompleteSubtitles, string incompleteText,
			int previousSubtitle) {

		if (!HasOnlyWhiteSpaces(incompleteText)) {
			IncompleteSubtitle incompleteSubtitle = new IncompleteSubtitle(previousSubtitle, incompleteText);
			incompleteSubtitles.Add(incompleteSubtitle);
		}
	}
コード例 #9
0
    private int ReadBodyEnd (string text, int bodyIndex, SubtitleFormat format,
    		SubtitleCollection collection, IncompleteSubtitleCollection incompleteSubtitles) {

    	Regex bodyEnd = new Regex(format.BodyEndIn + @"\s*", RegexOptions.IgnoreCase);
    	Match bodyEndMatch = bodyEnd.Match(text, bodyIndex);
    	if (bodyEndMatch.Success) {
	   		AddIncompleteSubtitleIfExists(text, bodyEndMatch, bodyIndex, collection.Count, incompleteSubtitles);
    		bodyIndex = bodyEndMatch.Index + bodyEndMatch.Length;
    	}
    	return bodyIndex;
	}
コード例 #10
0
	private int ReadSubtitles (string text, int bodyIndex, int textLength, Regex subtitleRegex, SubtitleFormat format,
		ParsingProperties properties, SubtitleCollection collection, IncompleteSubtitleCollection incompleteSubtitles) {

		Subtitle previousSubtitle = null;

		/* Read the subtitles. BodyIndex points to the start of the subtitles, skipping its possible beginning text*/
		while (bodyIndex < textLength) {
			Match match = subtitleRegex.Match(text, bodyIndex);
			if (match.Success) {
    			Subtitle subtitle = ParseSubtitle(match, format, properties, previousSubtitle);
    			collection.Add(subtitle);
				AddIncompleteSubtitleIfExists(text, match, bodyIndex, collection.Count, incompleteSubtitles);
	    		bodyIndex = match.Index + match.Length;
				previousSubtitle = subtitle;
   			}
   			else
    			break;
   		}
   		return bodyIndex;
   	}