diff options
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | Cargo.toml | 5 | ||||
| -rw-r--r-- | src/document_tree.rs | 284 | ||||
| -rw-r--r-- | src/document_tree/attribute_types.rs | 25 | ||||
| -rw-r--r-- | src/document_tree/element_categories.rs | 59 | ||||
| -rw-r--r-- | src/document_tree/element_types.rs | 96 | ||||
| -rw-r--r-- | src/document_tree/elements.rs | 207 | ||||
| -rw-r--r-- | src/document_tree/extra_attributes.rs | 56 | ||||
| -rw-r--r-- | src/document_tree/mod.rs | 21 | ||||
| -rw-r--r-- | src/lib.rs | 15 | ||||
| -rw-r--r-- | src/rst.rs | 6 |
11 files changed, 483 insertions, 293 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e9e2199 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target/ +/Cargo.lock @@ -4,6 +4,5 @@ name = "rst" version = "0.0.0" authors = [ "Phil Schaf <flying-sheep@web.de>" ] -[[bin]] - -name = "rst"
\ No newline at end of file +[dependencies] +url = "0.2.37" diff --git a/src/document_tree.rs b/src/document_tree.rs deleted file mode 100644 index 4502354..0000000 --- a/src/document_tree.rs +++ /dev/null @@ -1,284 +0,0 @@ -///http://docutils.sourceforge.net/docs/ref/doctree.html -///serves as AST - -//bools usually are XML yesorno. “auto” however either exists and is set to something random like “1” or doesn’t exist - -#![feature(struct_inherit)] - -use url::Url; - -//----------\\ -//Categories\\ -//----------\\ - -trait IElement; //TODO: needed? - -///A document’s direct children -trait SubDocument: IElement; -///body elements, topics, sidebars, transitions -trait SubStructure: SubDocument; -trait SubSection: IElement; -//title and body elements -trait SubTopic: IElement; -trait SubSidebar: IElement; -trait SubDLItem: IElement; -trait SubField: IElement; -trait SubOption: IElement; -trait SubLineBlock: IElement; -trait SubBlockQuote: IElement; -//label and body elements -trait SubFootnote: IElement; - -//-----------------\\ -//Element hierarchy\\ -//-----------------\\ - -virtual struct Element: IElement { - ids: Vec<String>, - names: Vec<String>, - //left out dupnames - source, - classes, -}; - - -//TODO: restructure so that elements with and without children derive from common structs - -virtual struct BodyOrStructuralElement: Element; - -virtual struct StructuralElement: BodyOrStructuralElement; -virtual struct StructuralSubElement: SubDocument; - -virtual struct BodyElement: BodyOrStructuralElement, SubStructure, SubSection, SubTopic, SubSidebar, SubBlockQuote, SubFootnote; -virtual struct SimpleBodyElement: BodyElement; -virtual struct CompoundBodyElement: BodyElement; - - -virtual struct BodySubElement: Element; - - -virtual struct BibliographicElement: Element; - - -virtual struct DecorationElement: Element; - - -virtual struct TextOrInlineElement: Element; -virtual struct InlineElement: TextOrInlineElement; - - -//--------------\\ -//Content Models\\ -//--------------\\ - -trait HasChildren<C: Element> { - fn add_child(C), -} - -trait TextModel: HasChildren<TextOrInlineElement>; -trait StructureModel: HasChildren<SubStructure>; -trait DocumentModel: HasChildren<SubDocument>; -trait InfoModel: HasChildren<BibliographicElement>; -trait AuthorsModel: HasChildren<AuthorInfo>; -trait DecorationModel: HasChildren<DecorationElement>; -trait BodyModel: HasChildren<BodyElement>; -trait SectionModel: HasChildren<SubSection>; -trait TopicModel: HasChildren<SubTopic>; -trait SidebarModel: HasChildren<SubSidebar>; - -trait ListModel: HasChildren<ListItem>; - -trait DLModel: HasChildren<DefinitionListItem>; -trait DLItemModel: HasChildren<SubDLItem>; - -trait FieldListModel: HasChildren<Field>; -trait FieldModel: HasChildren<SubField>; - -trait OptionListModel: HasChildren<OptionListItem>; -trait OptionGroupModel: HasChildren<Option_>; -trait OptionModel: HasChildren<SubOption>; - -trait BlockQuoteModel: HasChildren<SubBlockQuote>; -trait FootnoteModel: HasChildren<SubFootnote>; -trait FigureModel: HasChildren<SubFigure>; - -//--------\\ -//Elements\\ -//--------\\ - -struct Document: StructuralElement, DocumentModel; -struct Section: StructuralElement, SectionModel; -struct Topic: StructuralElement, TopicModel, SubStructure, SubSection, SubSidebar; -struct Sidebar: StructuralElement, SidebarModel, SubStructure, SubSection; - - -struct Title: StructuralSubElement, TextModel, SubSidebar, SubSection, SubTopic; -struct Subtitle: StructuralSubElement, TextModel, SubSidebar; -struct Decoration: StructuralSubElement, DecorationModel; -struct Docinfo: StructuralSubElement, InfoModel; -struct Transition: StructuralSubElement, SubStructure, SubSection; - - -struct Author: BibliographicElement, TextModel, AuthorInfo; -struct Authors: BibliographicElement, AuthorsModel; -struct Organization: BibliographicElement, TextModel, AuthorInfo; -struct Address: BibliographicElement, TextModel, AuthorInfo { space: FixedSpace }; -struct Contact: BibliographicElement, TextModel, AuthorInfo; -struct Version: BibliographicElement, TextModel; -struct Revision: BibliographicElement, TextModel; -struct Status: BibliographicElement, TextModel; -struct Date: BibliographicElement, TextModel; -struct Copyright: BibliographicElement, TextModel; -struct Field: BibliographicElement, FieldModel; - - -struct Header: DecorationElement, BodyModel; -struct Footer: DecorationElement, BodyModel; - - -struct Paragraph: SimpleBodyElement, TextModel; -struct LiteralBlock: SimpleBodyElement, TextModel { space: FixedSpace }; -struct DoctestBlock: SimpleBodyElement, TextModel { space: FixedSpace }; -struct MathBlock: SimpleBodyElement; -struct Rubric: SimpleBodyElement, TextModel; -struct SubstitutionDefinition: SimpleBodyElement, TextModel { ltrim: bool, rtrim: bool } -struct Comment: SimpleBodyElement, TextModel { space: FixedSpace }; -struct Pending: SimpleBodyElement; -struct Target: SimpleBodyElement { refuri: Url, refid: ID, refname: Vec<NameToken>, anonymous: bool } -struct Raw: SimpleBodyElement { space: FixedSpace, format: Vec<NameToken> } -struct Image: SimpleBodyElement, SubFigure { - align: AlignHV, - uri: Url, - alt: String, - height: Measure, - width: Measure, - scale: float, -} - - -struct Compound: CompoundBodyElement, BodyModel; -struct Container: CompoundBodyElement, BodyModel; - -struct BulletList: CompoundBodyElement, ListModel { bullet: String }; -struct EnumeratedList: CompoundBodyElement, ListModel { enumtype: EnumeratedListType, prefix: String, suffix: String } -struct DefinitionList: CompoundBodyElement, DLModel; -struct FieldList: CompoundBodyElement, FieldListModel; -struct OptionList: CompoundBodyElement, OptionListModel; - -struct LineBlock: CompoundBodyElement, LineBlockModel, SubLineBlock; -struct BlockQuote: CompoundBodyElement, BlockQuoteModel; -struct Admonition: CompoundBodyElement, TopicModel; -struct Attention: CompoundBodyElement, BodyModel; -struct Hint: CompoundBodyElement, BodyModel; -struct Note: CompoundBodyElement, BodyModel; -struct Caution: CompoundBodyElement, BodyModel; -struct Danger: CompoundBodyElement, BodyModel; -struct Error: CompoundBodyElement, BodyModel; -struct Important: CompoundBodyElement, BodyModel; -struct Tip: CompoundBodyElement, BodyModel; -struct Warning: CompoundBodyElement, BodyModel; -struct Footnote: CompoundBodyElement, FootnoteModel { backrefs: Vec<ID>, auto: bool } -struct Citation: CompoundBodyElement, FootnoteModel { backrefs: Vec<ID> } -struct SystemMessage: CompoundBodyElement, BodyModel { backrefs: Vec<ID>, level: uint, line: uint, type_: NameToken } -struct Figure: CompoundBodyElement, FigureModel { align: AlignH, width: uint } -struct Table: CompoundBodyElement; //TODO - - -struct ListItem: BodySubElement, BodyModel; - -struct DefinitionListItem: BodySubElement, DLItemModel; -struct Term: BodySubElement, TextModel, SubDLItem; -struct Classifier: BodySubElement, TextModel, SubDLItem; -struct Definition: BodySubElement, BodyModel, SubDLItem; - -struct FieldName: BodySubElement, TextModel, SubField; -struct FieldBody: BodySubElement, BodyModel, SubField; - -struct OptionList: BodySubElement, OptionListModel; -struct OptionListItem: BodySubElement, OptionListItemModel; -struct OptionGroup: BodySubElement, OptionGroupModel; -struct Description: BodySubElement, BodyModel; -struct Option_: BodySubElement, OptionModel; -struct OptionString: BodySubElement, TextModel, SubOption; -struct OptionArgument: BodySubElement, TextModel, SubOption { delimiter: String }; - -struct Line: BodySubElement, TextModel, SubLineBlock; -struct Attribution: BodySubElement, TextModel, SubBlockQuote; -struct Label_: BodySubElement, TextModel, SubFootnote; - -struct Caption: BodySubElement, TextModel, SubFigure; -struct Legend: BodySubElement, BodyModel, SubFigure; - -struct Emphasis: InlineElement, TextModel; -struct Strong: InlineElement, TextModel; -struct Literal: InlineElement, TextModel; -struct Reference: InlineElement, TextModel { name: String, refuri: Url, refid: ID, refname: Vec<NameToken> } -struct FootnoteReference: InlineElement, TextModel { refid: ID, refname: Vec<NameToken>, auto: bool } -struct CitationReference: InlineElement, TextModel { refid: ID, refname: Vec<NameToken> } -struct SubstitutionReference: InlineElement, TextModel { refname: Vec<NameToken> } -struct TitleReference: InlineElement, TextModel; -struct Abbreviation: InlineElement, TextModel; -struct Acronym: InlineElement, TextModel; -struct Superscript: InlineElement, TextModel; -struct Subscript: InlineElement, TextModel; -struct Inline: InlineElement, TextModel; -struct Problematic: InlineElement, TextModel { refid: ID } -struct Generated: InlineElement, TextModel; -struct Math: InlineElement; - -//also have non-inline versions. Inline image is no figure child, inline target has content -struct TargetInline: InlineElement, TextModel { refuri: Url, refid: ID, refname: Vec<NameToken>, anonymous: bool } -struct RawInline: InlineElement { space: FixedSpace, format: Vec<NameToken> } -struct ImageInline: InlineElement { - align: AlignHV, - uri: Url, - alt: String, - height: Measure, - width: Measure, - scale: float, -} - -struct TextElement: TextOrInlineElement; - - -//------\\ -//Others\\ -//------\\ - - -enum EnumeratedListType { - Arabic, - LowerAlpha, - UpperAlpha, - LowerRoman, - UpperRoman, -} - -enum FixedSpace { Default, Preserve } - -enum AlignH { Left, Center, Right} -enum AlignHV { Top, Middle, Bottom, Left, Center, Right } - -struct ID(String); -struct NameToken(String); - -enum Measure { - Pixel(uint), - Em(uint), - //TODO -} - -//----\\ -//impl\\ -//----\\ - -impl Default for Element { - fn default() -> Element -} - -impl Field { - get_name(&self) { - let name_elem = self.children[0]; - assert!(name_elem.t) - } -} diff --git a/src/document_tree/attribute_types.rs b/src/document_tree/attribute_types.rs new file mode 100644 index 0000000..83ce618 --- /dev/null +++ b/src/document_tree/attribute_types.rs @@ -0,0 +1,25 @@ +#[derive(Debug)] +pub enum EnumeratedListType { + Arabic, + LowerAlpha, + UpperAlpha, + LowerRoman, + UpperRoman, +} + +#[derive(Debug)] +pub enum FixedSpace { Default, Preserve } // yes, default really is not “Default” +impl Default for FixedSpace { fn default() -> FixedSpace { FixedSpace::Preserve } } + +#[derive(Debug)] pub enum AlignH { Left, Center, Right} +#[derive(Debug)] pub enum AlignHV { Top, Middle, Bottom, Left, Center, Right } + +#[derive(Debug)] pub struct ID(String); +#[derive(Debug)] pub struct NameToken(String); + +#[derive(Debug)] +pub enum Measure { + Pixel(usize), + Em(usize), + //TODO +} diff --git a/src/document_tree/element_categories.rs b/src/document_tree/element_categories.rs new file mode 100644 index 0000000..5f12cc6 --- /dev/null +++ b/src/document_tree/element_categories.rs @@ -0,0 +1,59 @@ +use super::elements::*; + +pub trait HasChildren<C> { + fn add_child<R: Into<C>>(&mut self, R); +} + +macro_rules! synonymous_enum {( $name:ident { $( $entry:ident ),* } ) => ( + #[derive(Debug)] + pub enum $name { + $( + $entry($entry), + )* + } + + $( + impl Into<$name> for $entry { + fn into(self) -> $name { + $name::$entry(self) + } + } + )* +)} + +synonymous_enum!(SubStructure { Topic, Sidebar, Transition, Section, BodyElement }); +synonymous_enum!(StructuralSubElement { Title, Subtitle, Decoration, Docinfo, Transition, SubStructure }); +synonymous_enum!(BodyElement { + //Simple + Paragraph, LiteralBlock, DoctestBlock, MathBlock, Rubric, SubstitutionDefinition, Comment, Pending, Target, Raw, Image, + //Compound + Compound, Container, + BulletList, EnumeratedList, DefinitionList, FieldList, OptionList, + LineBlock, BlockQuote, Admonition, Attention, Hint, Note, Caution, Danger, Error, Important, Tip, Warning, Footnote, Citation, SystemMessage, Figure, Table +}); + +synonymous_enum!(BibliographicElement { Author, Authors, Organization, Address, Contact, Version, Revision, Status, Date, Copyright, Field }); + +synonymous_enum!(TextOrInlineElement { + TextElement, Emphasis, Strong, Literal, Reference, FootnoteReference, CitationReference, SubstitutionReference, TitleReference, Abbreviation, Acronym, Superscript, Subscript, Inline, Problematic, Generated, Math, + //also have non-inline versions. Inline image is no figure child, inline target has content + TargetInline, RawInline, ImageInline +}); + +//--------------\\ +//Content Models\\ +//--------------\\ + +synonymous_enum!(SubSection { Title, Subtitle, Docinfo, Decoration, SubStructure, BodyElement }); +synonymous_enum!(AuthorInfo { Author, Organization, Address, Contact }); +synonymous_enum!(DecorationElement { Header, Footer }); +synonymous_enum!(SubTopic { Title, BodyElement }); +synonymous_enum!(SubSidebar { Topic, Title, Subtitle, BodyElement }); +synonymous_enum!(SubDLItem { Term, Classifier, Definition }); +synonymous_enum!(SubField { FieldName, FieldBody }); +synonymous_enum!(SubOptionListItem { OptionGroup, Description }); +synonymous_enum!(SubOption { OptionString, OptionArgument }); +synonymous_enum!(SubLineBlock { LineBlock, Line }); +synonymous_enum!(SubBlockQuote { Attribution, BodyElement }); +synonymous_enum!(SubFootnote { Label_, BodyElement }); +synonymous_enum!(SubFigure { Image, Caption, Legend, BodyElement }); diff --git a/src/document_tree/element_types.rs b/src/document_tree/element_types.rs new file mode 100644 index 0000000..232d44f --- /dev/null +++ b/src/document_tree/element_types.rs @@ -0,0 +1,96 @@ + +// enum ElementType { +// //structual elements +// Section, Topic, Sidebar, +// +// //structural subelements +// Title, Subtitle, Decoration, Docinfo, Transition, +// +// //bibliographic elements +// Author, Authors, Organization, +// Address { space: FixedSpace }, +// Contact, Version, Revision, Status, +// Date, Copyright, Field, +// +// //decoration elements +// Header, Footer, +// +// //simple body elements +// Paragraph, +// LiteralBlock { space: FixedSpace }, +// DoctestBlock { space: FixedSpace }, +// MathBlock, Rubric, +// SubstitutionDefinition { ltrim: bool, rtrim: bool }, +// Comment { space: FixedSpace }, +// Pending, +// Target { refuri: Url, refid: ID, refname: Vec<NameToken>, anonymous: bool }, +// Raw { space: FixedSpace, format: Vec<NameToken> }, +// Image { +// align: AlignHV, +// uri: Url, +// alt: String, +// height: Measure, +// width: Measure, +// scale: f64, +// }, +// +// //compound body elements +// Compound, Container, +// +// BulletList { bullet: String }, +// EnumeratedList { enumtype: EnumeratedListType, prefix: String, suffix: String }, +// DefinitionList, FieldList, OptionList, +// +// LineBlock, BlockQuote, +// Admonition, Attention, Hint, Note, +// Caution, Danger, Error, Important, +// Tip, Warning, +// Footnote { backrefs: Vec<ID>, auto: bool }, +// Citation { backrefs: Vec<ID> }, +// SystemMessage { backrefs: Vec<ID>, level: usize, line: usize, type_: NameToken }, +// Figure { align: AlignH, width: usize }, +// Table, //TODO +// +// //body sub elements +// ListItem, +// +// DefinitionListItem, Term, +// Classifier, Definition, +// +// FieldName, FieldBody, +// +// OptionListItem, OptionGroup, Description, Option_, OptionString, +// OptionArgument { delimiter: String }, +// +// Line, Attribution, Label_, +// +// Caption, Legend, +// +// //inline elements +// Emphasis, Strong, Literal, +// Reference { name: String, refuri: Url, refid: ID, refname: Vec<NameToken> }, +// FootnoteReference { refid: ID, refname: Vec<NameToken>, auto: bool }, +// CitationReference { refid: ID, refname: Vec<NameToken> }, +// SubstitutionReference { refname: Vec<NameToken> }, +// TitleReference, +// Abbreviation, Acronym, +// Superscript, Subscript, +// Inline, +// Problematic { refid: ID }, +// Generated, Math, +// +// //also have non-inline versions. Inline image is no figure child, inline target has content +// TargetInline { refuri: Url, refid: ID, refname: Vec<NameToken>, anonymous: bool }, +// RawInline { space: FixedSpace, format: Vec<NameToken> }, +// ImageInline { +// align: AlignHV, +// uri: Url, +// alt: String, +// height: Measure, +// width: Measure, +// scale: f64, +// }, +// +// //text element +// TextElement, +// } diff --git a/src/document_tree/elements.rs b/src/document_tree/elements.rs new file mode 100644 index 0000000..e6709b4 --- /dev/null +++ b/src/document_tree/elements.rs @@ -0,0 +1,207 @@ +use url::Url; + +use super::extra_attributes::{self,ExtraAttributes}; +use super::element_categories::*; + + +//-----------------\\ +//Element hierarchy\\ +//-----------------\\ + +pub trait Element { + fn ids(&self) -> &Vec<String>; + fn names(&self) -> &Vec<String>; + fn source(&self) -> &Option<Url>; + fn classes(&self) -> &Vec<String>; +} + +#[derive(Default,Debug)] +pub struct CommonAttributes { + ids: Vec<String>, + names: Vec<String>, + //left out dupnames + source: Option<Url>, + classes: Vec<String>, +} + +//----\\ +//impl\\ +//----\\ + +macro_rules! impl_element(($name:ident) => { + impl Element for $name { + fn ids(&self) -> &Vec<String> { &self.common.ids } + fn names(&self) -> &Vec<String> { &self.common.names } + fn source(&self) -> &Option<Url> { &self.common.source } + fn classes(&self) -> &Vec<String> { &self.common.classes } + } +}); + +macro_rules! impl_children(($name:ident, $childtype:ident) => { + impl HasChildren<$childtype> for $name { + fn add_child<R: Into<$childtype>>(&mut self, child: R) { + self.children.push(Box::new(child.into())); + } + } +}); + +macro_rules! impl_extra(($name:ident) => { + impl ExtraAttributes<extra_attributes::$name> for $name { + fn extra(&self) -> &extra_attributes::$name { &self.extra } + } +}); + +macro_rules! impl_elem( + ($name:ident) => { + #[derive(Default,Debug)] + pub struct $name { common: CommonAttributes } + impl_element!($name); + }; + ($name:ident; +) => { + #[derive(Default,Debug)] + pub struct $name { common: CommonAttributes, extra: extra_attributes::$name } + impl_element!($name); impl_extra!($name); + }; + ($name:ident; *) => { //same as above with no default + #[derive(Debug)] + pub struct $name { common: CommonAttributes, extra: extra_attributes::$name } + impl_element!($name); impl_extra!($name); + }; + ($name:ident, $childtype:ident) => { + #[derive(Default,Debug)] + pub struct $name { common: CommonAttributes, children: Vec<Box<$childtype>> } + impl_element!($name); impl_children!($name, $childtype); + }; + ($name:ident, $childtype:ident; +) => { + #[derive(Default,Debug)] + pub struct $name { common: CommonAttributes, extra: extra_attributes::$name, children: Vec<Box<$childtype>> } + impl_element!($name); impl_extra!($name); impl_children!($name, $childtype); + }; +); + +macro_rules! impl_elems(( $( ($($args:tt)*) )* ) => { + $( impl_elem!($($args)*); )* +}); + +impl_elems!( + //structual elements + (Section, SubSection) + (Topic, SubTopic) + (Sidebar, SubSidebar) + + //structural subelements + (Title, TextOrInlineElement) + (Subtitle, TextOrInlineElement) + (Decoration, DecorationElement) + (Docinfo, BibliographicElement) + (Transition) + + //bibliographic elements + (Author, TextOrInlineElement) + (Authors, AuthorInfo) + (Organization, TextOrInlineElement) + (Address, TextOrInlineElement; +) + (Contact, TextOrInlineElement) + (Version, TextOrInlineElement) + (Revision, TextOrInlineElement) + (Status, TextOrInlineElement) + (Date, TextOrInlineElement) + (Copyright, TextOrInlineElement) + (Field, SubField) + + //decoration elements + (Header, BodyElement) + (Footer, BodyElement) + + //simple body elements + (Paragraph, TextOrInlineElement) + (LiteralBlock, TextOrInlineElement; +) + (DoctestBlock, TextOrInlineElement; +) + (MathBlock) + (Rubric, TextOrInlineElement) + (SubstitutionDefinition, TextOrInlineElement; +) + (Comment, TextOrInlineElement; +) + (Pending) + (Target; +) + (Raw; +) + (Image; *) + + //compound body elements + (Compound, BodyElement) + (Container, BodyElement) + + (BulletList, ListItem; +) + (EnumeratedList, ListItem; +) + (DefinitionList, DefinitionListItem) + (FieldList, Field) + (OptionList, OptionListItem) + + (LineBlock, SubLineBlock) + (BlockQuote, SubBlockQuote) + (Admonition, SubTopic) + (Attention, BodyElement) + (Hint, BodyElement) + (Note, BodyElement) + (Caution, BodyElement) + (Danger, BodyElement) + (Error, BodyElement) + (Important, BodyElement) + (Tip, BodyElement) + (Warning, BodyElement) + (Footnote, SubFootnote; +) + (Citation, SubFootnote; +) + (SystemMessage, BodyElement; +) + (Figure, SubFigure; +) + (Table; +) //TODO + + //body sub elements + (ListItem, BodyElement) + + (DefinitionListItem, SubDLItem) + (Term, TextOrInlineElement) + (Classifier, TextOrInlineElement) + (Definition, BodyElement) + + (FieldName, TextOrInlineElement) + (FieldBody, BodyElement) + + (OptionListItem, SubOptionListItem) + (OptionGroup, Option_) + (Description, BodyElement) + (Option_, SubOption) + (OptionString, TextOrInlineElement) + (OptionArgument, TextOrInlineElement; +) + + (Line, TextOrInlineElement) + (Attribution, TextOrInlineElement) + (Label_, TextOrInlineElement) + + (Caption, TextOrInlineElement) + (Legend, BodyElement) + + //inline elements + (Emphasis, TextOrInlineElement) + (Literal, TextOrInlineElement) + (Reference, TextOrInlineElement; +) + (Strong, TextOrInlineElement) + (FootnoteReference, TextOrInlineElement; +) + (CitationReference, TextOrInlineElement; +) + (SubstitutionReference, TextOrInlineElement; +) + (TitleReference, TextOrInlineElement) + (Abbreviation, TextOrInlineElement) + (Acronym, TextOrInlineElement) + (Superscript, TextOrInlineElement) + (Subscript, TextOrInlineElement) + (Inline, TextOrInlineElement) + (Problematic, TextOrInlineElement; +) + (Generated, TextOrInlineElement) + (Math) + + //also have non-inline versions. Inline image is no figure child, inline target has content + (TargetInline, TextOrInlineElement; +) + (RawInline; +) + (ImageInline; *) + + //text element + (TextElement) +); diff --git a/src/document_tree/extra_attributes.rs b/src/document_tree/extra_attributes.rs new file mode 100644 index 0000000..2141469 --- /dev/null +++ b/src/document_tree/extra_attributes.rs @@ -0,0 +1,56 @@ +use url::Url; + +use super::attribute_types::{FixedSpace,ID,NameToken,AlignHV,AlignH,Measure,EnumeratedListType}; + +pub trait ExtraAttributes<A> { + fn extra(&self) -> &A; +} + +#[derive(Default,Debug)] pub struct Address { pub space: FixedSpace } +#[derive(Default,Debug)] pub struct LiteralBlock { pub space: FixedSpace } +#[derive(Default,Debug)] pub struct DoctestBlock { pub space: FixedSpace } +#[derive(Default,Debug)] pub struct SubstitutionDefinition { pub ltrim: Option<bool>, pub rtrim: Option<bool> } +#[derive(Default,Debug)] pub struct Comment { pub space: FixedSpace } +#[derive(Default,Debug)] pub struct Target { pub refuri: Option<Url>, pub refid: Option<ID>, pub refname: Vec<NameToken>, pub anonymous: Option<bool> } +#[derive(Default,Debug)] pub struct Raw { pub space: FixedSpace, pub format: Vec<NameToken> } +#[derive(Debug)] +pub struct Image { + pub align: Option<AlignHV>, + pub uri: Url, + pub alt: Option<String>, + pub height: Option<Measure>, + pub width: Option<Measure>, + pub scale: Option<f64>, +} + +//bools usually are XML yesorno. “auto” however either exists and is set to something random like “1” or doesn’t exist + +#[derive(Default,Debug)] pub struct BulletList { pub bullet: Option<String> } +#[derive(Default,Debug)] pub struct EnumeratedList { pub enumtype: Option<EnumeratedListType>, pub prefix: Option<String>, pub suffix: Option<String> } + +#[derive(Default,Debug)] pub struct Footnote { pub backrefs: Vec<ID>, pub auto: Option<bool> } +#[derive(Default,Debug)] pub struct Citation { pub backrefs: Vec<ID> } +#[derive(Default,Debug)] pub struct SystemMessage { pub backrefs: Vec<ID>, pub level: Option<usize>, pub line: Option<usize>, pub type_: Option<NameToken> } +#[derive(Default,Debug)] pub struct Figure { pub align: Option<AlignH>, pub width: Option<usize> } +#[derive(Default,Debug)] pub struct Table; //TODO + +#[derive(Default,Debug)] pub struct OptionArgument { pub delimiter: Option<String> } + +#[derive(Default,Debug)] pub struct Reference { pub name: Option<String>, pub refuri: Option<Url>, pub refid: Option<ID>, pub refname: Vec<NameToken> } +#[derive(Default,Debug)] pub struct FootnoteReference { pub refid: Option<ID>, pub refname: Vec<NameToken>, pub auto: Option<bool> } +#[derive(Default,Debug)] pub struct CitationReference { pub refid: Option<ID>, pub refname: Vec<NameToken> } +#[derive(Default,Debug)] pub struct SubstitutionReference { pub refname: Vec<NameToken> } +#[derive(Default,Debug)] pub struct Problematic { pub refid: Option<ID> } + +//also have non-inline versions. Inline image is no figure child, inline target has content +#[derive(Default,Debug)] pub struct TargetInline { pub refuri: Option<Url>, pub refid: Option<ID>, pub refname: Vec<NameToken>, pub anonymous: Option<bool> } +#[derive(Default,Debug)] pub struct RawInline { pub space: FixedSpace, pub format: Vec<NameToken> } +#[derive(Debug)] +pub struct ImageInline { + pub align: Option<AlignHV>, + pub uri: Url, + pub alt: Option<String>, + pub height: Option<Measure>, + pub width: Option<Measure>, + pub scale: Option<f64>, +} diff --git a/src/document_tree/mod.rs b/src/document_tree/mod.rs new file mode 100644 index 0000000..868b9f8 --- /dev/null +++ b/src/document_tree/mod.rs @@ -0,0 +1,21 @@ +///http://docutils.sourceforge.net/docs/ref/doctree.html +///serves as AST + +pub mod elements; +pub mod element_categories; +pub mod extra_attributes; +pub mod attribute_types; + +use self::element_categories::StructuralSubElement; + +pub use self::elements::*; //Element,CommonAttributes, +pub use self::extra_attributes::ExtraAttributes; +pub use self::element_categories::HasChildren; + +#[derive(Default,Debug)] +pub struct Document { children: Vec<Box<StructuralSubElement>> } +impl HasChildren<StructuralSubElement> for Document { + fn add_child<R: Into<StructuralSubElement>>(&mut self, child: R) { + self.children.push(Box::new(child.into())); + } +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..33ced85 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,15 @@ +extern crate url; + +pub mod document_tree; + +#[test] +fn test() { + use document_tree as dt; + use document_tree::HasChildren; + + let mut doc = dt::Document::default(); + let title = dt::Title::default(); + doc.add_child(title); + + println!("{:?}", doc); +} diff --git a/src/rst.rs b/src/rst.rs deleted file mode 100644 index 33f95c4..0000000 --- a/src/rst.rs +++ /dev/null @@ -1,6 +0,0 @@ -use document_tree as dt; - -fn main() { - let mut doc = dt::Document::new(); - doc.add_child(dt::Title::default()) -} |
