1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
//! docx を読むところ
//!
//! 試すファイルは、本文の XML を手で書いてこの中で組み立てる。
mod common;
use folio::document::{Block, Document};
use folio::paragraph::{Align, Paragraph};
use std::path::PathBuf;
/// 試しに使うファイルの置き場。target の下なので消えても差し支えない
fn place(name: &str) -> PathBuf {
let mut path = PathBuf::from("target");
path.push(format!("document-{name}.docx"));
path
}
/// 本文を読み切る
fn collect(document: &mut Document) -> Vec<Block> {
let mut body = document.read().unwrap();
let mut result = Vec::new();
while let Some(block) = body.read().unwrap() {
result.push(block);
}
result
}
/// 段落だけを取り出す
fn paragraph(block: &Block) -> &Paragraph {
match block {
Block::Paragraph(one) => one,
Block::Table(_) => panic!("段落ではありません"),
}
}
/// 文字 1 つだけの段落
fn line(text: &str) -> String {
format!("<w:p><w:r><w:t>{text}</w:t></w:r></w:p>")
}
/// 文字 1 つだけのセル
fn cell(text: &str) -> String {
format!("<w:tc>{}</w:tc>", line(text))
}
#[test]
fn paragraphs_come_back_with_their_style_and_runs() {
let path = place("paragraph");
let body = concat!(
"<w:p><w:pPr><w:pStyle w:val=\"Title\"/></w:pPr><w:r><w:t>題</w:t></w:r></w:p>",
"<w:p><w:pPr><w:pStyle w:val=\"Heading1\"/></w:pPr><w:r><w:t>見出し</w:t></w:r></w:p>",
"<w:p><w:pPr><w:jc w:val=\"center\"/><w:rPr><w:b/></w:rPr></w:pPr>",
"<w:r><w:t>ふつう</w:t></w:r>",
"<w:r><w:rPr><w:b/></w:rPr><w:t>太字</w:t></w:r>",
"<w:r><w:rPr><w:i/></w:rPr><w:t>斜体</w:t></w:r>",
"<w:r><w:rPr><w:u w:val=\"single\"/></w:rPr><w:t>下線</w:t></w:r>",
"<w:r><w:rPr><w:strike/></w:rPr><w:t>取り消し</w:t></w:r>",
"<w:r><w:rPr><w:sz w:val=\"28\"/></w:rPr><w:t>大きい</w:t></w:r>",
"<w:r><w:rPr><w:rFonts w:ascii=\"Meiryo\"/><w:color w:val=\"FF0000\"/></w:rPr><w:t>赤</w:t></w:r>",
"<w:r><w:rPr><w:b w:val=\"0\"/></w:rPr><w:t>太字でない</w:t></w:r>",
"</w:p>",
);
common::document(&path, body);
let mut document = Document::open(&path).unwrap();
let block = collect(&mut document);
assert_eq!(block.len(), 3);
assert_eq!(paragraph(&block[0]).style, "Title");
assert_eq!(paragraph(&block[0]).text(), "題");
assert_eq!(paragraph(&block[1]).style, "Heading1");
let one = paragraph(&block[2]);
assert_eq!(one.align, Align::Center);
assert_eq!(one.run.len(), 8);
assert!(!one.run[0].bold, "段落の印の飾りは、続きの文字の飾りではない");
assert!(one.run[1].bold);
assert!(one.run[2].italic);
assert!(one.run[3].underline);
assert!(one.run[4].strike);
assert_eq!(one.run[5].size, 14.0);
assert_eq!(one.run[6].color, "FF0000");
assert_eq!(one.run[6].font, "Meiryo");
assert!(!one.run[7].bold);
assert_eq!(one.text(), "ふつう太字斜体下線取り消し大きい赤太字でない");
}
#[test]
fn marks_and_breaks_come_back() {
let path = place("mark");
let body = concat!(
"<w:p><w:r><w:t xml:space=\"preserve\">記号 <&>\"' です</w:t></w:r></w:p>",
"<w:p><w:r><w:t>改行</w:t><w:br/><w:t>と</w:t><w:tab/><w:t>タブ</w:t></w:r></w:p>",
);
common::document(&path, body);
let mut document = Document::open(&path).unwrap();
let block = collect(&mut document);
assert_eq!(paragraph(&block[0]).text(), "記号 <&>\"' です");
assert_eq!(paragraph(&block[1]).text(), "改行\nと\tタブ");
}
#[test]
fn a_table_comes_back_with_its_cells() {
let path = place("table");
let body = format!(
"<w:tbl><w:tblPr><w:tblStyle w:val=\"TableGrid\"/><w:tblBorders><w:top w:val=\"single\"/></w:tblBorders></w:tblPr>\
<w:tblGrid><w:gridCol/><w:gridCol/><w:gridCol/></w:tblGrid>\
<w:tr><w:tc><w:tcPr><w:gridSpan w:val=\"2\"/></w:tcPr>{}</w:tc>{}</w:tr>\
<w:tr>{}{}{}</w:tr></w:tbl>\
{}\
<w:tbl><w:tr>{}</w:tr></w:tbl>",
line("つながり"),
cell("三"),
cell("あ"),
cell("い"),
cell("う"),
line("表のあと"),
cell("枠なし"),
);
common::document(&path, &body);
let mut document = Document::open(&path).unwrap();
let block = collect(&mut document);
assert_eq!(block.len(), 3);
let Block::Table(one) = &block[0] else {panic!("表ではありません")};
assert_eq!(one.style, "TableGrid");
assert!(one.border);
assert_eq!(one.row.len(), 2);
assert_eq!(one.row[0].cell[0].span, 2);
assert_eq!(one.row[0].cell[0].text(), "つながり");
assert_eq!(one.row[0].cell[1].text(), "三");
assert_eq!(one.row[1].cell.len(), 3);
assert_eq!(one.row[1].cell[2].text(), "う");
assert_eq!(one.width(), 3);
assert_eq!(paragraph(&block[1]).text(), "表のあと");
let Block::Table(two) = &block[2] else {panic!("表ではありません")};
assert!(!two.border, "表そのものに枠線の指定がなければ false");
}
#[test]
fn a_table_can_hold_another_table() {
let path = place("nested");
let body = format!(
"<w:tbl><w:tr><w:tc>{}<w:tbl><w:tr>{}</w:tr></w:tbl><w:p/></w:tc></w:tr></w:tbl>",
line("そと"),
cell("おく"),
);
common::document(&path, &body);
let mut document = Document::open(&path).unwrap();
let block = collect(&mut document);
let Block::Table(one) = &block[0] else {panic!("表ではありません")};
let inside = &one.row[0].cell[0].block;
assert_eq!(inside.len(), 3, "段落、表、空の段落");
assert_eq!(inside[2], Block::Paragraph(Paragraph::default()));
let Block::Table(deep) = &inside[1] else {panic!("中に表がありません")};
assert_eq!(deep.row[0].cell[0].text(), "おく");
}
#[test]
fn the_whole_text_can_be_taken_at_once() {
let path = place("text");
let body = format!("{}<w:tbl><w:tr>{}{}</w:tr></w:tbl>{}", line("一行目"), cell("あ"), cell("い"), line("三行目"));
common::document(&path, &body);
let mut document = Document::open(&path).unwrap();
let text = document.text().unwrap();
assert!(text.contains("一行目"));
assert!(text.contains("あ\tい"));
assert!(text.contains("三行目"));
}
#[test]
fn a_section_break_does_not_get_in_the_way() {
let path = place("section");
let body = format!(
"{}<w:p><w:pPr><w:sectPr><w:pgSz w:w=\"1\" w:h=\"1\"/></w:sectPr></w:pPr></w:p>\
<w:sectPr><w:pgSz w:w=\"16838\" w:h=\"11906\" w:orient=\"landscape\"/></w:sectPr>",
line("まえ"),
);
common::document(&path, &body);
let mut document = Document::open(&path).unwrap();
let block = collect(&mut document);
assert_eq!(block.len(), 2);
assert_eq!(paragraph(&block[0]).text(), "まえ");
assert_eq!(paragraph(&block[1]).text(), "");
}
#[test]
fn many_paragraphs_are_read_in_order() {
let path = place("many");
let count = 30000;
let body: String = (0..count).map(|index| line(&format!("{index} 番目の段落です。"))).collect();
common::document(&path, &body);
let mut document = Document::open(&path).unwrap();
let mut body = document.read().unwrap();
let mut seen = 0;
while let Some(block) = body.read().unwrap() {
assert_eq!(block.text(), format!("{seen} 番目の段落です。"));
seen += 1;
}
assert_eq!(seen, count);
}
#[test]
fn a_file_that_is_not_a_document_is_an_error() {
let path = place("broken");
std::fs::write(&path, "これは docx ではありません").unwrap();
assert!(Document::open(&path).is_err());
}