Exemplo n.º 1
0
        public void TestParseYml2()
        {
            string f       = @"../../abstract-classes3.md";
            var    content = File.ReadAllText(f);
            var    result  = YMLMeister.ParseYML2(content, null);

            foreach (var v in result)
            {
                Debug.WriteLine(v.TagName);
                foreach (var v2 in v.TagValues)
                {
                    Debug.WriteLine("   " + v2);
                }
            }
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            // Load options
            var     opts           = new Options(args);
            var     currentFile    = "";
            var     currentContent = "";
            var     currentBody    = "";
            var     currentTagList = new Tags();
            Command command        = null;
            string  commandFile    = "";

            try
            {
                commandFile = File.ReadAllText(opts.ArgFile);
            }
            catch (Exception e)
            {
                Console.WriteLine("Unable to read command file {0}", opts.ArgFile);
                Console.WriteLine(e.Message);
                Console.WriteLine("Press any key to continue...");
                Console.ReadLine();
                System.Environment.Exit(1);
            }

            var commandRecords = commandFile.Split('\n');

            foreach (var commandRecord in commandRecords)
            {
                var trimmedCommand = commandRecord.Trim();
                if (!String.IsNullOrEmpty(trimmedCommand) && trimmedCommand != CommandBuilder.Header)
                {
                    try
                    {
                        command = new Command(trimmedCommand, opts.OptUnique);
                    }
                    catch (Exception e)
                    {
                        string warning = String.Format("Unable to read command {0} in command file {1}", trimmedCommand, opts.ArgFile);
                        MUT.MutLog.AppendWarning(warning);
                        MUT.MutLog.AppendError(e.Message);
                        continue;
                    }

                    if (!String.IsNullOrEmpty(currentFile) && currentFile != command.Filename)
                    {
                        WriteCurrentFile(opts, currentFile, currentBody, currentTagList);
                    }

                    if (currentFile != command.Filename)
                    {
                        currentFile = command.Filename;

                        try
                        {
                            currentContent = File.ReadAllText(currentFile);
                        }
                        catch (System.IO.FileNotFoundException e)
                        {
                            // Log it?
                            MUT.MutLog.AppendError(e.Message);
                            continue;
                        }
                        catch (System.IO.DirectoryNotFoundException e)
                        {
                            // Log it?
                            MUT.MutLog.AppendError(e.Message);
                            continue;
                        }
                        // param 2 only used in mdextract to specify just get one tag from each file.
                        // For apply we need to get all tags because any unchanged vals need to be written back into file
                        currentTagList = new Tags(YMLMeister.ParseYML2(currentContent, null));
                        currentBody    = currentContent.Substring(currentContent.IndexOf("---", 4) + 3);
                    }

                    switch (command.TagAction)
                    {
                    case Command.Action.create:
                        AddTagIfNotPresent(currentTagList, command);
                        break;

                    case Command.Action.delete:
                        DeleteTag(currentTagList, command);
                        break;

                    case Command.Action.overwrite:
                        OverwriteIfTagExists(currentTagList, command);
                        break;

                    case Command.Action.require:
                        OverwriteOrAddTag(currentTagList, command);
                        break;

                    case Command.Action.excise:
                        ExciseValues(currentTagList, command);
                        break;

                    case Command.Action.merge_if:
                        MergeValuesIfTagExists(currentTagList, command);
                        break;

                    case Command.Action.merge_add:
                        MergeValuesOrAddTag(currentTagList, command);
                        break;

                    default:
                        MUT.MutLog.AppendInfo(String.Format("Ignoring the {0} tag in {1}",
                                                            command.TagData.TagName, command.Filename));
                        break;
                    }
                }
            }

            if (command != null && !String.IsNullOrEmpty(currentFile))
            {
                // We're done, write the file out, if there's anything to write.
                WriteCurrentFile(opts, currentFile, currentBody, currentTagList);
            }

            // open commands file or report failure and exit
            // create objects for applies-to file, metadata collection
            // for each line in commands file
            //   load fields from line or report failure and continue
            //   if first line or filename field is not the current applies-to file
            //     if filename field is not the current applies-to file
            //       if there's an updated metadata collection
            //         write the metadata collection to the applies-to file or report failure
            //       close current applies-to file
            //     open new applies-to file or report failure and continue
            //     parse metadata collection from applies-to file or report failure and continue
            //   apply action to metadata collection or report failure and continue
            // if there's an open applies-to file
            //   if there's an updated metadata collection
            //     write the metadata collection to the applies-to file or report failure
            //   close current applies-to file
            // report complete and exit

            if (opts.Use_log)
            {
                MUT.MutLog.WriteFile(opts.Logfile());
            }

            Console.WriteLine("Mdapply: Complete.");
            // Console.ReadLine();
        }