Esempio n. 1
0
        private void btn_Create_Click(object sender, EventArgs e)
        {
            if (Directory.Exists(baseDir))
            {
                if (string.IsNullOrWhiteSpace(tBox_FileName.Text))
                {
                    MessageBox.Show("请输入文件名");
                    return;
                }
                string fileName = tBox_FileName.Text.Trim().Replace(' ', '_');
                string filepath = Path.Combine(baseDir, fileName);
                if (filepath.Substring(filepath.Length - 2).ToUpper() != ".MD")
                {
                    filepath += ".md";
                }
                if (File.Exists(filepath))
                {
                    MessageBox.Show("文件名已经存在!");
                    return;
                }
                StreamWriter     sw       = File.CreateText(filepath);
                Entitys.BlogHead blogHead = new Entitys.BlogHead();
                blogHead.type        = "";
                blogHead.tags        = new List <string>();
                blogHead.title       = fileName;
                blogHead.photos      = new List <string>();
                blogHead.description = "";
                blogHead.date        = DateTime.Now;
                StringBuilder headStr = new StringBuilder();

                headStr.AppendLine("---StartBlogHead");
                headStr.AppendLine(JsonPrase.PraseToJson(Newtonsoft.Json.JsonConvert.SerializeObject(blogHead)));
                //headStr.AppendLine("{");
                //headStr.AppendLine(string.Format("    \"title\": \"{0}\",", fileName));
                //headStr.AppendLine("    \"date\": \"0001-01-01T00:00:00\",");
                //headStr.AppendLine("    \"type\": \"\",");
                //headStr.AppendLine("    \"tags\": [],");
                //headStr.AppendLine("    \"photos\": [],");
                //headStr.AppendLine("    \"description\": \"\"");
                //headStr.AppendLine("}");
                headStr.AppendLine("---EndBlogHead");
                headStr.AppendLine("");
                headStr.AppendLine("##Hello World!");

                sw.Write(headStr.ToString());
                sw.Close();
                DelLeftTreeEvent();
                this.Close();
            }
            else
            {
                MessageBox.Show("请重新选择文件目录!");
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 递归编译
        /// </summary>
        /// <param name="blogViewLiat"></param>
        /// <returns></returns>
        private int CompileList(List <FileEntity> blogViewLiat)
        {
            int count = 0;

            foreach (FileEntity item in blogViewLiat)
            {
                if (item.GetName() == "README.MD")
                {
                    continue;
                }
                if (item.GetFileType() == FileType.Directory)
                {
                    if (item.GetList() != null && item.GetList().Count > 0)
                    {
                        count += CompileList(item.GetList());
                    }
                    continue;
                }

                // md文件创建日期
                DateTime createTime = File.GetCreationTime(item.GetFullPath());

                // 获取md文件内容
                string mdStr = FileHelper.ReadFile(item.GetFullPath());

                // 获取blog头部的配置
                string           mdhead   = mdStr.Substring(mdStr.IndexOf("---StartBlogHead") + 16, mdStr.IndexOf("---EndBlogHead") - 16);
                Entitys.BlogHead blogHead = Newtonsoft.Json.JsonConvert.DeserializeObject <Entitys.BlogHead>(mdhead);

                // 获取blog内容
                string mdBody = mdStr.Substring(mdStr.IndexOf("---EndBlogHead") + 14);

                // 根据md文件创建日期获取html文件创建路径
                string relativePath = Path.Combine(new string[] { createTime.Year.ToString(), createTime.Month.ToString(), createTime.Day.ToString(), item.GetName() });
                string dirStr       = Path.Combine(Program.GetBlogDir(), relativePath);

                // html 文件的所在深度
                int deep = relativePath.Count(c => c == '\\') + 1;

                // 获取html文件的相对路径
                string relativeUrl  = "" + relativePath.Replace("\\", "/");
                string relativeHead = "";
                for (int i = 0; i < deep; i++)
                {
                    relativeHead += "../";
                }

                relativeUrl = relativeHead + relativeUrl;

                StringBuilder blogFootStr = new StringBuilder();
                blogFootStr.AppendLine("");
                blogFootStr.AppendLine("<p>");
                blogFootStr.AppendLine(string.Format("<a title =\"pre\" class=\"prev-article\" href=\"{0}\" > 上一篇</a>", "#"));
                blogFootStr.AppendLine(string.Format("<a title=\"next\" class=\"next-article\" href=\"#{0}\">下一篇</a>", "#"));
                blogFootStr.AppendLine("</p>");
                // 将md文件转换成html

                MarkdownSharp.Markdown md = new MarkdownSharp.Markdown();
                string articleStr         = string.Format("<article><h1 class=\"article-title\">{0}</h1>\n\r\n\r{1}\n\r\n\r{2}</article>", blogHead.title, md.Transform(mdBody), blogFootStr.ToString());
                //string articleStr = string.Format("<article><h1 class=\"article-title\">{0}</h1>\n\r\n\r{1}\n\r\n\r{2}</article>", blogHead.title, MarkDownHelper.ConvertToHtml(mdBody), blogFootStr.ToString());

                // 创建html文件存放路径
                if (!Directory.Exists(dirStr))
                {
                    Directory.CreateDirectory(dirStr);
                }

                // html文件路径
                string pathStr = Path.Combine(dirStr, "index.html");

                //样式文件的引用 todo:处理本地文件和非本地文件非关系
                string styleRef = "\n\r<link rel=\"stylesheet\" type=\"text/css\" href=\"" + relativeHead + "Styles/style.css\" />";
                styleRef  = string.Join("\r\n", Program.GetConfig().Site.headref.Where(str => !str.Contains("href=\"http://") & !str.Contains("href=\"https://") & !str.Contains("src=\"http://") & !str.Contains("src=\"https://"))).Replace("href=\"", "href=\"" + relativeHead);
                styleRef += string.Join("\r\n", Program.GetConfig().Site.headref.Where(str => str.Contains("href=\"http://") || str.Contains("href=\"https://") || str.Contains("src=\"http://") || str.Contains("src=\"https://")));
                if (FileHelper.WriteFile(string.Format(htmlModel, headStr + styleRef, articleStr), pathStr))
                {
                    count++;
                    BlogView blogView = new BlogView();
                    blogView.tags       = new List <string>();
                    blogView.title      = blogHead.title; //todo: edit
                    blogView.edittime   = DateTime.Now;
                    blogView.createtime = createTime;
                    blogView.describe   = blogHead.description;

                    // 计算HashCode
                    var    hash = System.Security.Cryptography.HashAlgorithm.Create();
                    var    fs   = File.Open(item.GetFullPath(), FileMode.Open);
                    byte[] bts  = hash.ComputeHash(fs);
                    fs.Close();
                    blogView.hashcode = string.Join("-", bts);
                    blogView.id       = item.GetId();
                    blogView.num      = item.GetId();
                    blogView.tags     = blogHead.tags;
                    blogView.type     = blogHead.type;
                    blogView.url      = relativeUrl;
                    blogView.imgurls  = blogHead.photos;
                    this.blogListView.bloglist.Add(blogView);

                    indexHtml.AppendLine("");
                    indexHtml.AppendLine("<p>");
                    indexHtml.AppendLine(string.Format("    <a title=\"{0}\" target=\"_self\" href=\"{1}\\\">", blogView.title, relativeUrl.Replace("../", "")));
                    indexHtml.AppendLine(string.Format("        <h3>{0}</h3>", blogView.title));
                    indexHtml.AppendLine(string.Format("        <h4>{0}</h4>", blogView.describe));
                    foreach (string imgurl in blogView.imgurls)
                    {
                        indexHtml.AppendLine(string.Format("        <img alt=\"{0}\" src=\"{1}\" />", blogView.title, imgurl));
                    }
                    indexHtml.AppendLine("    </a>");
                    indexHtml.AppendLine("</p>");
                    if (!typeList.Contains(blogView.type))
                    {
                        typeList.Add(blogView.type);
                    }
                    tagList.Concat(blogView.tags);
                    //if (!tagList.Concat(blogView.tags))
                    //    tagList.Add(blogView.tag);
                }
            }
            return(count);
        }