= 0) //是<打头的params,例如
这种
23 | {
24 | tagB = xmlParams.IndexOfAny(spaces, tagB + 1);
25 | tagE = xmlParams.IndexOf('>', tagB);
26 | }
27 | else
28 | {
29 | tagB = 0;
30 | tagE = xmlParams.Length;
31 | }
32 |
33 | char[] seps = new char[] {' ', '\t'};
34 | string t = xmlParams.Substring(tagB, tagE - tagB);
35 | string[] items = t.Split(seps, StringSplitOptions.RemoveEmptyEntries);
36 |
37 | Dictionary
ret = new Dictionary();
38 |
39 | char[] seps2 = new char[] { '=' };
40 | char[] seps3 = new char[] { '"', ' ', '\n', '\r', '\t' };
41 | foreach (string item in items)
42 | {
43 | string[] values = item.Split(seps2, StringSplitOptions.RemoveEmptyEntries);
44 | if (values.Length != 2)
45 | {
46 | continue;
47 | }
48 |
49 | ret.Add(values[0].Trim(), values[1].Trim(seps3));
50 | }
51 |
52 | return ret;
53 | }
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/wecode.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thinkry/wecode/2e91f25751f8276d08ec14743a9d7faee6cb4483/wecode.ico
--------------------------------------------------------------------------------
/youdao/YouDaoNode2.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 | using System.Web;
5 |
6 | //第2版本的有道云笔记的节点类
7 | namespace WeCode1._0.youdao
8 | {
9 | class YouDaoNode2
10 | {
11 | public const string typeBase64 = "base64";
12 |
13 | private string _path = ""; //该对象在有道云的path,可理解为key
14 | private string _title = ""; //标题
15 | private string _content = ""; //正文
16 | private Dictionary _contentParams = new Dictionary(); //正文的参数信息 updatetime/type等
17 | private List> _attachments = new List>(); //附件
18 |
19 | public YouDaoNode2()
20 | {
21 | _contentParams["type"] = typeBase64;
22 | }
23 |
24 | public string GetPath()
25 | {
26 | return _path;
27 | }
28 |
29 | public void SetPath(string value)
30 | {
31 | _path = value;
32 | }
33 |
34 | public string GetTitle()
35 | {
36 | return _title;
37 | }
38 |
39 | public void SetTitle(string value)
40 | {
41 | _title = value;
42 | }
43 |
44 | public string GetContent()
45 | {
46 | return _content;
47 | }
48 |
49 | public void SetContent(string value)
50 | {
51 | _content = value;
52 | }
53 |
54 | public string GetUpdateTime()
55 | {
56 | string result = _contentParams["updatetime"];
57 | return (result != null) ? result.Trim() : result;
58 | }
59 |
60 | public void SetUpdateTime(string updatetime)
61 | {
62 | _contentParams["updatetime"] = updatetime;
63 | }
64 |
65 | public List> GetAttachments()
66 | {
67 | return _attachments;
68 | }
69 |
70 | public void AddAttachment(string fileUrl, string fileLength, string fileName)
71 | {
72 | Dictionary img = new Dictionary();
73 | img.Add("src", "");
74 | img.Add("alt", fileName);
75 | img.Add("title", fileName);
76 | img.Add("filename", "");
77 | img.Add("filelength", fileLength);
78 | img.Add("path", fileUrl);
79 | img.Add("id", "");
80 | _attachments.Add(img);
81 | }
82 |
83 | public bool DelAttachment(string fileUrl)
84 | {
85 | foreach (Dictionary img in _attachments)
86 | {
87 | if (fileUrl == img["path"])
88 | {
89 | _attachments.Remove(img);
90 | return true;
91 | }
92 | }
93 |
94 | return false;
95 | }
96 |
97 | public bool RenameAttachment(string fileUrl, string fileName)
98 | {
99 | foreach (Dictionary img in _attachments)
100 | {
101 | if (fileUrl == img["path"])
102 | {
103 | img["title"] = fileName;
104 | img["alt"] = fileName;
105 | return true;
106 | }
107 | }
108 |
109 | return false;
110 | }
111 |
112 | ///
113 | /// 解析data,返回YouDaoNode2
114 | ///
115 | ///
116 | public static YouDaoNode2 FromString(string path, string data, string createTime)
117 | {
118 | YouDaoNode2 result = new YouDaoNode2();
119 | result.SetPath(path);
120 |
121 | //获取笔记的配置信息
122 | Dictionary contentParams = utils.StringUtils.SplitXmlParams(data);
123 |
124 | //解析内容 第一个div标记
125 | int firstB = data.IndexOf('>') + 1;
126 | int firstE = data.IndexOf("", firstB, StringComparison.OrdinalIgnoreCase);
127 | string first = data.Substring(firstB, firstE - firstB);
128 |
129 | if (contentParams.ContainsKey("type") && contentParams["type"].Trim() == typeBase64)
130 | {
131 | result.SetContent(Encoding.Default.GetString(Convert.FromBase64String(first)));
132 | }
133 | else
134 | {
135 | string t = HttpUtility.HtmlDecode(first);
136 | t = t.Replace("\n
", "\n"); //未采用base64编码前,有道云会把\n替换为\n
,所以这里要处理下
137 | result.SetContent(t);
138 | }
139 |
140 | //获取最后更新时间
141 | if (contentParams.ContainsKey("updatetime") && contentParams["updatetime"].Length > 0)
142 | {
143 | result.SetUpdateTime(contentParams["updatetime"]);
144 | }
145 | else
146 | {
147 | result.SetUpdateTime(createTime);
148 | }
149 |
150 | //复制剩下的contentParam, type和updatetime已在上面处理
151 | foreach (string key in contentParams.Keys)
152 | {
153 | if (key != "type" && key != "updatetime")
154 | {
155 | result._contentParams[key] = contentParams[key];
156 | }
157 | }
158 |
159 | //解析附件第二个div标记
160 | int secondB = data.IndexOf("
= 0) //有附件
162 | {
163 | secondB = data.IndexOf('>', secondB + 4) + 1;
164 | int secondE = data.LastIndexOf("
", StringComparison.OrdinalIgnoreCase);
165 | string second = data.Substring(secondB, secondE - secondB);
166 | result.ParseAttachments(second);
167 | }
168 |
169 | return result;
170 | }
171 |
172 | ///
173 | /// 拼装内容和附件组成一个字符串,用于提交到有道云
174 | ///
175 | ///
176 | public string ToString()
177 | {
178 | StringBuilder result = new StringBuilder();
179 |
180 | //拼装内容
181 | result.Append("
kv in _contentParams)
183 | {
184 | result.AppendFormat(" {0}=\"{1}\"", kv.Key, kv.Value);
185 | }
186 | result.AppendFormat(">\n{0}\n
\n", Convert.ToBase64String(Encoding.Default.GetBytes(_content)));
187 |
188 | //拼装附件
189 | if (_attachments.Count > 0)
190 | {
191 | result.Append("
\n");
192 | foreach (Dictionary
attachment in _attachments)
193 | {
194 | result.Append("
kv in attachment)
196 | {
197 | result.AppendFormat(" {0}=\"{1}\"", kv.Key, kv.Value);
198 | }
199 | result.Append(">\n");
200 | }
201 | result.Append("\n");
202 | }
203 |
204 | return result.ToString();
205 | }
206 |
207 | private void ParseAttachments(string str)
208 | {
209 | str = str.Trim();
210 | string[] sep = new string[] { "
![]()
', '/', ' ', '\r', '\n', '\t' };
214 | foreach (string item in items)
215 | {
216 | string s = item.Trim(sep2);
217 | _attachments.Add(utils.StringUtils.SplitXmlParams(s));
218 | }
219 | }
220 | }
221 | }
222 |
--------------------------------------------------------------------------------