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
use crate::error::Result;
use crate::model::{
CtrlHeader, ListHeader, PageDef, ParaCharShape, ParaLineSeg, ParaText, Paragraph, Section,
SectionDef,
};
use crate::parser::record::{HwpTag, Record};
use crate::reader::StreamReader;
use crate::utils::compression::decompress_stream;
pub struct BodyTextParser;
impl BodyTextParser {
pub fn parse(data: Vec<u8>, is_compressed: bool) -> Result<BodyText> {
let data = if is_compressed {
decompress_stream(&data)?
} else {
data
};
let mut reader = StreamReader::new(data);
let mut sections = Vec::new();
let mut current_section = Section::default();
let mut current_paragraph: Option<Paragraph> = None;
let mut first_section = true;
while reader.remaining() >= 4 {
// Need at least 4 bytes for record header
let record = match Record::parse(&mut reader) {
Ok(r) => r,
Err(_) => break, // Stop parsing on error
};
match HwpTag::from_u16(record.tag_id()) {
// Page Definition - only appears once at the beginning
Some(HwpTag::PageDef) => {
current_section.page_def = PageDef::from_record(&record).ok();
}
// SectionDefine (0x42) - Actually marks paragraph start in this document
Some(HwpTag::SectionDefine) => {
// First one is the actual section definition
if first_section {
current_section.section_def = SectionDef::from_record(&record).ok();
first_section = false;
} else {
// Subsequent ones mark new paragraphs
if let Some(para) = current_paragraph.take() {
current_section.paragraphs.push(para);
}
current_paragraph = Some(Paragraph::default());
}
}
// Tag 0x43 - Contains text content
Some(HwpTag::ColumnDefine) => {
if let Some(ref mut para) = current_paragraph {
if let Ok(text) = ParaText::from_record(&record) {
para.text = Some(text);
}
}
}
// TableControl (0x44) - Contains paragraph properties
Some(HwpTag::TableControl) => {
if let Some(ref mut para) = current_paragraph {
// Try to parse as paragraph header
if let Ok(new_para) = Paragraph::from_header_record(&record) {
// Copy properties from parsed paragraph
para.control_mask = new_para.control_mask;
para.para_shape_id = new_para.para_shape_id;
para.style_id = new_para.style_id;
para.column_type = new_para.column_type;
para.char_shape_count = new_para.char_shape_count;
para.range_tag_count = new_para.range_tag_count;
para.line_align_count = new_para.line_align_count;
para.instance_id = new_para.instance_id;
}
}
}
// Standard paragraph records (if they exist)
Some(HwpTag::ParaHeader) => {
if let Some(para) = current_paragraph.take() {
current_section.paragraphs.push(para);
}
if let Ok(para) = Paragraph::from_header_record(&record) {
current_paragraph = Some(para);
}
// Skip invalid paragraph headers
}
Some(HwpTag::ParaText) => {
if let Some(ref mut para) = current_paragraph {
para.text = Some(ParaText::from_record(&record)?);
}
}
Some(HwpTag::ParaCharShape) => {
if let Some(ref mut para) = current_paragraph {
para.char_shapes = ParaCharShape::from_record(&record).ok();
}
}
Some(HwpTag::ParaLineSeg) => {
if let Some(ref mut para) = current_paragraph {
para.line_segments = ParaLineSeg::from_record(&record).ok();
}
}
// Control Records
Some(HwpTag::ListHeader) => {
if let Some(ref mut para) = current_paragraph {
para.list_header = ListHeader::from_record(&record).ok();
}
}
Some(HwpTag::CtrlHeader) => {
if let Some(ref mut para) = current_paragraph {
para.ctrl_header = CtrlHeader::from_record(&record).ok();
}
}
// ParaRangeTag (0x54) - Contains hyperlink information
Some(HwpTag::ParaRangeTag) => {
if let Some(ref mut para) = current_paragraph {
// Try to parse as hyperlink
if let Ok(hyperlink) =
crate::model::hyperlink::Hyperlink::from_record(&record)
{
para.hyperlinks.push(hyperlink);
}
}
}
_ => {
// Skip other tags for now
}
}
}
// Add last paragraph and section
if let Some(para) = current_paragraph {
current_section.paragraphs.push(para);
}
// Always add the section even if empty - there's at least one section
sections.push(current_section);
Ok(BodyText { sections })
}
}
#[derive(Debug, Default)]
pub struct BodyText {
pub sections: Vec<Section>,
}
impl BodyText {
pub fn extract_text(&self) -> String {
let mut result = String::new();
for section in &self.sections {
for para in §ion.paragraphs {
if let Some(ref text) = para.text {
result.push_str(&text.content);
result.push('\n');
}
}
}
result
}
}