Example #1
0
        private void UpdateIndex(FullPost post)
        {
            Field name = new Field("Name", post.Name, Field.Store.YES, Field.Index.NOT_ANALYZED);
            Field title = new Field("Title", post.Title, Field.Store.NO, Field.Index.ANALYZED);
            Field content = new Field("Content", post.Content, Field.Store.NO, Field.Index.ANALYZED);
            Field tags = new Field("Tags", String.Join(" ", post.Tags), Field.Store.NO, Field.Index.ANALYZED);

            Document document = new Document();
            document.Add(name);
            document.Add(title);
            document.Add(content);
            document.Add(tags);

            Indexer.UpdateDocument(new Term("Name", post.Name), document);
            Indexer.Flush(true, true, true);
            Indexer.Optimize();
        }
Example #2
0
        private void UpdatePost(string name, FullPost post)
        {
            string filename = Server.MapPath("~/Content/" + name + ".md");
            string[] lines = System.IO.File.ReadAllLines(filename);
            post.Name = name;
            post.Title = lines[0].Split(':')[1].Trim();
            post.PostDate = DateTime.ParseExact(lines[2].Split(':')[1].Trim(), "yyyy-MM-dd", null);
            post.UpdateDate = DateTime.Today;
            post.Tags = lines[1].Split(':')[1].Split(',').Select(t => t.Trim()).ToArray();

            StringBuilder excerpt = new StringBuilder();
            StringBuilder content = new StringBuilder();
            bool isExcerptRecorded = false;
            for (int i = 3; i < lines.Length; i++) {
                string line = lines[i];
                if (line.Trim() == "<!-- more -->") {
                    isExcerptRecorded = true;
                }

                content.AppendLine(line);
                if (!isExcerptRecorded) {
                    excerpt.AppendLine(line);
                }
            }
            post.Excerpt = excerpt.ToString().Trim();
            post.Content = content.ToString().Trim();
        }
Example #3
0
        public void Update(string name)
        {
            FullPost post = DbSession.Get<FullPost>(name);
            HashSet<string> storedTags = new HashSet<string>();
            if (post == null) {
                post = new FullPost();
            }
            else {
                storedTags.UnionWith(post.Tags);
            }
            UpdatePost(name, post);

            // 有以前的tag列表说明数据库里有这一篇,使用更新
            if (storedTags.Count > 0) {
                DbSession.Update(post);
            }
            else {
                DbSession.Save(post);
            }

            // 抽取tag的区别
            HashSet<string> currentTags = new HashSet<string>(post.Tags);
            UpdateTags(currentTags, storedTags);

            // 更新全文索引
            UpdateIndex(post);
        }