| 1 |
use crate::errors::{ErrorKind, Result}; |
| 2 |
use crate::fsshttpb::data::exguid::ExGuid; |
| 3 |
use crate::one::property::layout_alignment::LayoutAlignment; |
| 4 |
use crate::one::property_set::{page_manifest_node, page_metadata, page_node, title_node}; |
| 5 |
use crate::onenote::outline::{parse_outline, Outline}; |
| 6 |
use crate::onenote::page_content::{parse_page_content, PageContent}; |
| 7 |
use crate::onestore::object_space::ObjectSpace; |
| 8 |
|
| 9 |
/// A page. |
| 10 |
/// |
| 11 |
/// See [\[MS-ONE\] 1.3.2] and [\[MS-ONE\] 2.2.19]. |
| 12 |
/// |
| 13 |
/// [\[MS-ONE\] 1.3.2]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/2dd687ac-f36b-4723-b959-4d60c8a90ca9 |
| 14 |
/// [\[MS-ONE\] 2.2.19]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/e381b7c7-b434-43a2-ba23-0d08bafd281a |
| 15 |
#[derive(Clone, Debug)] |
| 16 |
pub struct Page { |
| 17 |
title: Option<Title>, |
| 18 |
level: i32, |
| 19 |
author: Option<String>, |
| 20 |
height: Option<f32>, |
| 21 |
contents: Vec<PageContent>, |
| 22 |
} |
| 23 |
|
| 24 |
impl Page { |
| 25 |
/// The page's title element. |
| 26 |
/// |
| 27 |
/// See [\[MS-ONE\] 2.2.64]. |
| 28 |
/// |
| 29 |
/// [\[MS-ONE\] 2.2.64]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/00f0b68b-db49-4aea-9ad9-7c8e68e5c95d |
| 30 |
pub fn title(&self) -> Option<&Title> { |
| 31 |
self.title.as_ref() |
| 32 |
} |
| 33 |
|
| 34 |
/// The page's level in the section page tree. |
| 35 |
/// |
| 36 |
/// See [\[MS-ONE\] 2.3.74]. |
| 37 |
/// |
| 38 |
/// [\[MS-ONE\] 2.3.74]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/a8632c90-e74a-4ef6-8852-707d4c8817cd |
| 39 |
pub fn level(&self) -> i32 { |
| 40 |
self.level |
| 41 |
} |
| 42 |
|
| 43 |
/// The page's author. |
| 44 |
pub fn author(&self) -> Option<&str> { |
| 45 |
self.author.as_deref() |
| 46 |
} |
| 47 |
|
| 48 |
/// The page's height. |
| 49 |
/// |
| 50 |
/// See [\[MS-ONE\] 2.3.7]. |
| 51 |
/// |
| 52 |
/// [\[MS-ONE\] 2.3.7]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/e5d0b5e0-0702-42af-8299-0e27c895ba7e |
| 53 |
pub fn height(&self) -> Option<f32> { |
| 54 |
self.height |
| 55 |
} |
| 56 |
|
| 57 |
/// The page contents. |
| 58 |
pub fn contents(&self) -> &[PageContent] { |
| 59 |
&self.contents |
| 60 |
} |
| 61 |
|
| 62 |
/// The page's title text. |
| 63 |
/// |
| 64 |
/// This is calculated using a heuristic similar to the one OneNote uses. |
| 65 |
pub fn title_text(&self) -> Option<&str> { |
| 66 |
self.title |
| 67 |
.as_ref() |
| 68 |
.and_then(|title| title.contents.first()) |
| 69 |
.and_then(Self::outline_text) |
| 70 |
.or_else(|| { |
| 71 |
self.contents |
| 72 |
.iter() |
| 73 |
.filter_map(|page_content| page_content.outline()) |
| 74 |
.filter_map(Self::outline_text) |
| 75 |
.next() |
| 76 |
}) |
| 77 |
} |
| 78 |
|
| 79 |
fn outline_text(outline: &Outline) -> Option<&str> { |
| 80 |
outline |
| 81 |
.items |
| 82 |
.first() |
| 83 |
.and_then(|outline_item| outline_item.element()) |
| 84 |
.and_then(|outline_element| outline_element.contents.first()) |
| 85 |
.and_then(|content| content.rich_text()) |
| 86 |
.and_then(|text| Some(&*text.text).filter(|s| !s.is_empty())) |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
/// A page title. |
| 91 |
/// |
| 92 |
/// See [\[MS-ONE\] 2.2.29]. |
| 93 |
/// |
| 94 |
/// [\[MS-ONE\] 2.2.29]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/08bd4fd5-59fb-4568-9c82-d2d5280eced8 |
| 95 |
|
| 96 |
#[derive(Clone, Debug)] |
| 97 |
pub struct Title { |
| 98 |
pub(crate) contents: Vec<Outline>, |
| 99 |
pub(crate) offset_horizontal: f32, |
| 100 |
pub(crate) offset_vertical: f32, |
| 101 |
pub(crate) layout_alignment_in_parent: Option<LayoutAlignment>, |
| 102 |
pub(crate) layout_alignment_self: Option<LayoutAlignment>, |
| 103 |
} |
| 104 |
|
| 105 |
impl Title { |
| 106 |
/// The title contents. |
| 107 |
pub fn contents(&self) -> &[Outline] { |
| 108 |
&self.contents |
| 109 |
} |
| 110 |
|
| 111 |
/// The horizontal offset from the page origin in half-inch increments. |
| 112 |
/// |
| 113 |
/// See [\[MS-ONE\] 2.3.18]. |
| 114 |
/// |
| 115 |
/// [\[MS-ONE\] 2.3.18]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/5fb9e84a-c9e9-4537-ab14-e5512f24669a |
| 116 |
pub fn offset_horizontal(&self) -> f32 { |
| 117 |
self.offset_horizontal |
| 118 |
} |
| 119 |
|
| 120 |
/// The vertical offset from the page origin in half-inch increments. |
| 121 |
/// |
| 122 |
/// See [\[MS-ONE\] 2.3.19]. |
| 123 |
/// |
| 124 |
/// [\[MS-ONE\] 2.3.19]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/5c4992ba-1db5-43e9-83dd-7299c562104d |
| 125 |
pub fn offset_vertical(&self) -> f32 { |
| 126 |
self.offset_vertical |
| 127 |
} |
| 128 |
|
| 129 |
/// The title's alignment relative to the containing outline element (if present). |
| 130 |
/// |
| 131 |
/// See [\[MS-ONE\] 2.3.27]. |
| 132 |
/// |
| 133 |
/// [\[MS-ONE\] 2.3.27]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/61fa50be-c355-4b8d-ac01-761a2f7f66c0 |
| 134 |
pub fn layout_alignment_in_parent(&self) -> Option<LayoutAlignment> { |
| 135 |
self.layout_alignment_in_parent |
| 136 |
} |
| 137 |
|
| 138 |
/// The title's alignment. |
| 139 |
/// |
| 140 |
/// See [\[MS-ONE\] 2.3.33]. |
| 141 |
/// |
| 142 |
/// [\[MS-ONE\] 2.3.33]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/4e7fe9db-2fdb-4239-b291-dc4b909c94ad |
| 143 |
pub fn layout_alignment_self(&self) -> Option<LayoutAlignment> { |
| 144 |
self.layout_alignment_self |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
pub(crate) fn parse_page(page_space: &ObjectSpace) -> Result<Page> { |
| 149 |
let metadata = parse_metadata(page_space)?; |
| 150 |
let manifest = parse_manifest(page_space)?; |
| 151 |
|
| 152 |
let data = parse_data(manifest, page_space)?; |
| 153 |
|
| 154 |
let title = data |
| 155 |
.title |
| 156 |
.map(|id| parse_title(id, page_space)) |
| 157 |
.transpose()?; |
| 158 |
let level = metadata.page_level; |
| 159 |
|
| 160 |
let contents = data |
| 161 |
.content |
| 162 |
.into_iter() |
| 163 |
.map(|content_id| parse_page_content(content_id, page_space)) |
| 164 |
.collect::<Result<_>>()?; |
| 165 |
|
| 166 |
Ok(Page { |
| 167 |
title, |
| 168 |
level, |
| 169 |
author: data.author.map(|author| author.into_value()), |
| 170 |
height: data.page_height, |
| 171 |
contents, |
| 172 |
}) |
| 173 |
} |
| 174 |
|
| 175 |
fn parse_title(title_id: ExGuid, space: &ObjectSpace) -> Result<Title> { |
| 176 |
let title_object = space |
| 177 |
.get_object(title_id) |
| 178 |
.ok_or_else(|| ErrorKind::MalformedOneNoteData("title object is missing".into()))?; |
| 179 |
let title = title_node::parse(title_object)?; |
| 180 |
let contents = title |
| 181 |
.children |
| 182 |
.into_iter() |
| 183 |
.map(|outline_id| parse_outline(outline_id, space)) |
| 184 |
.collect::<Result<_>>()?; |
| 185 |
|
| 186 |
Ok(Title { |
| 187 |
contents, |
| 188 |
offset_horizontal: title.offset_horizontal, |
| 189 |
offset_vertical: title.offset_vertical, |
| 190 |
layout_alignment_in_parent: title.layout_alignment_in_parent, |
| 191 |
layout_alignment_self: title.layout_alignment_self, |
| 192 |
}) |
| 193 |
} |
| 194 |
|
| 195 |
fn parse_data(manifest: page_manifest_node::Data, space: &ObjectSpace) -> Result<page_node::Data> { |
| 196 |
let page_id = manifest.page; |
| 197 |
let page_object = space |
| 198 |
.get_object(page_id) |
| 199 |
.ok_or_else(|| ErrorKind::MalformedOneNoteData("page object is missing".into()))?; |
| 200 |
|
| 201 |
page_node::parse(page_object) |
| 202 |
} |
| 203 |
|
| 204 |
fn parse_manifest(space: &ObjectSpace) -> Result<page_manifest_node::Data> { |
| 205 |
let page_manifest_id = space |
| 206 |
.content_root() |
| 207 |
.ok_or_else(|| ErrorKind::MalformedOneNoteData("page content id is missing".into()))?; |
| 208 |
let page_manifest_object = space |
| 209 |
.get_object(page_manifest_id) |
| 210 |
.ok_or_else(|| ErrorKind::MalformedOneNoteData("page object is missing".into()))?; |
| 211 |
|
| 212 |
page_manifest_node::parse(page_manifest_object) |
| 213 |
} |
| 214 |
|
| 215 |
fn parse_metadata(space: &ObjectSpace) -> Result<page_metadata::Data> { |
| 216 |
let metadata_id = space |
| 217 |
.metadata_root() |
| 218 |
.ok_or_else(|| ErrorKind::MalformedOneNoteData("page metadata id is missing".into()))?; |
| 219 |
let metadata_object = space |
| 220 |
.get_object(metadata_id) |
| 221 |
.ok_or_else(|| ErrorKind::MalformedOneNoteData("page metadata object is missing".into()))?; |
| 222 |
|
| 223 |
page_metadata::parse(metadata_object) |
| 224 |
} |