Пример #1
0
        private void SavePoemToFile(Poem poem)
        {
            // Todo: Check whether file name is already used and postfix with time based hash if needed to prevent duplication.

            string fullPath = System.IO.Path.Combine(App.PoemFolderPath, poem.Name);

            File.WriteAllLines(fullPath, poem.Chunks);
            RefreshPoemList();
        }
Пример #2
0
        /// <summary> Convert a string to a poem</summary>
        /// <param name="poemString"></param>
        /// <returns>poem</returns>
        public static Poem GetPoemFromString(string poemString)
        {
            Poem poem = null;

            if (!string.IsNullOrWhiteSpace(poemString))
            {
                // Split into lines with all possible CRLF variations
                string[] lines = Regex.Split(poemString, "\r\n|\r|\n");

                // Remove any blank lines at the top so we get a title
                while (string.IsNullOrWhiteSpace(lines[0]))
                {
                    lines = lines.Where(line => !string.IsNullOrWhiteSpace(line)).ToArray();
                }

                poem        = new Poem();
                poem.Chunks = lines.ToList();
            }

            return(poem);
        }
Пример #3
0
        private Poem ReadPoemFromFile(string filePath)
        {
            Poem poem = new Poem();

            try
            {
                poem.Chunks = File.ReadAllLines(filePath).ToList();
                if (poem.Chunks.Count == 0)
                {
                    poem.Chunks = new List <string> {
                        MiscText.EmptyPoemText
                    };
                    poem.IsEmpty = true;
                }
            }
            catch (System.Exception ex)
            {
                Log.Error(logTag, string.Format("Couldn't read poem from file {0} because {1}", filePath, ex.Message));
            }
            return(poem);
        }
Пример #4
0
        /// <summary>Convert a string to a list of poems. Poems delimited by BulkLoadDelimiter</summary>
        /// <param name="poemsString"></param>
        /// <returns>List of poems</returns>
        public static List <Poem> GetPoemsFromString(string poemsString)
        {
            List <Poem> poems = new List <Poem>();

            int startIdx = 0;
            int endIdx   = 0;

            MatchCollection matches = Regex.Matches(poemsString, App.BulkLoadDelimiter);

            // If no BulkLoadDelimiter, there's just one poem
            if (matches.Count == 0)
            {
                poems.Add(GetPoemFromString(poemsString));
            }
            else
            {
                Poem poem = null;
                // Create a poem from the string before each delimiter
                foreach (Match match in matches)
                {
                    endIdx = match.Index;
                    poem   = GetPoemFromString(poemsString.Substring(startIdx, endIdx - startIdx));
                    if (poem != null)
                    {
                        poems.Add(poem);
                    }
                    startIdx = endIdx + App.BulkLoadDelimiter.Length;
                }
                // Add the poem after the last delimiter
                poem = GetPoemFromString(poemsString.Substring(startIdx, poemsString.Length - startIdx));
                if (poem != null)
                {
                    poems.Add(poem);
                }
            }
            return(poems);
        }
Пример #5
0
 /// <summary> Try to fetch the poem identified by 'name'</summary>
 /// <param name="name"></param>
 /// <param name="poem"></param>
 /// <returns>true if successful</returns>
 public override bool TryGetPoem(string name, out Poem poem)
 {
     poem = null;
     return(false);
 }
Пример #6
0
 /// <summary> Try to fetch the poem identified by 'name'</summary>
 /// <param name="name"></param>
 /// <param name="poem"></param>
 /// <returns>true if successful</returns>
 public virtual bool TryGetPoem(string name, out Poem poem)
 {
     poem = null;
     return(false);
 }