Exemplo n.º 1
0
        /// <summary>
        /// Replaces a tag (such as "%year%") with an appropriate value (such as "2020").
        /// </summary>
        /// <param name="preview">Determines if this is a preview of a macro. We either use screen capture date/time or DateTime.Now depending on this boolean.</param>
        /// <param name="name">The name of a region or screen when parsing the %name% tag.</param>
        /// <param name="macro">The macro to parse. A macro usually includes tags such as %count% and %date%.</param>
        /// <param name="screenNumber">The screen number. For example, if this is the second display then the screen number is 2.</param>
        /// <param name="format">The image format to use as an image file extension when parsing the %format% tag.</param>
        /// <param name="activeWindowTitle">The title of the active window.</param>
        /// <param name="tag">The macro tag to use during parsing.</param>
        /// <returns>A parsed macro containing the appropriate values of respective tags in the provided macro.</returns>
        private static string ParseTag(bool preview, string name, string macro, int screenNumber, ImageFormat format, string activeWindowTitle, Tag tag)
        {
            int      count;
            DateTime dt;

            if (preview || screenCapture == null)
            {
                count = 1;
                dt    = DateTime.Now;
            }
            else
            {
                count = screenCapture.Count;
                dt    = screenCapture.DateTimeScreenshotsTaken;
            }

            // Strip out any backslash characters from the name so we avoid creating unnecessary directories based on the name.
            name = name.Replace("\\", string.Empty);

            if (!tag.Active)
            {
                return(macro);
            }

            switch (tag.Type)
            {
            case TagType.ActiveWindowTitle:
                macro = macro.Replace(tag.Name, activeWindowTitle);
                break;

            case TagType.DateTimeFormat:
                macro = macro.Replace(tag.Name, dt.ToString(tag.DateTimeFormatValue));
                break;

            case TagType.ImageFormat:
                macro = format != null && !string.IsNullOrEmpty(format.Name) ? macro.Replace(tag.Name, format.Name.ToLower()) : macro;
                break;

            case TagType.ScreenCaptureCycleCount:
                macro = macro.Replace(tag.Name, count.ToString());
                break;

            case TagType.ScreenName:
                macro = !string.IsNullOrEmpty(name) ? macro.Replace(tag.Name, name) : macro;
                break;

            case TagType.ScreenNumber:
                macro = macro.Replace(tag.Name, screenNumber.ToString());
                break;

            case TagType.User:
                macro = macro.Replace(tag.Name, Environment.UserName);
                break;

            case TagType.Machine:
                macro = macro.Replace(tag.Name, Environment.MachineName);
                break;

            case TagType.DateTimeFormatExpression:
                macro = macro.Replace(tag.Name,
                                      MacroTagExpressionParser.ParseTagExpressionForDateTimeFormat(dt, tag.DateTimeFormatValue));
                break;
            }

            return(StripInvalidWindowsCharacters(macro));
        }
Exemplo n.º 2
0
 /// <summary>
 /// The Macro Parser is responsible for parsing some given text looking for "macro tags" and responding with the appropriate value for each macro tag.
 /// </summary>
 /// <param name="settings"></param>
 public MacroParser(Settings settings)
 {
     _settings = settings;
     _macroTagExpressionParser = new MacroTagExpressionParser();
 }