summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2018-04-10 21:57:11 +0200
committerMatthias Beyer <mail@beyermatthias.de>2018-04-15 10:27:09 +0200
commitbb0d4319c392d75458adb6871db89a1666e70313 (patch)
tree1d8b7198417bc7f74d63b52de2d498328ddcf3ec /lib
parent90eb83a538885d8ea548e2f9d6842528d7164cf0 (diff)
Initial import: libimagwiki
Diffstat (limited to 'lib')
-rw-r--r--lib/domain/libimagwiki/Cargo.toml32
l---------lib/domain/libimagwiki/README.md1
-rw-r--r--lib/domain/libimagwiki/src/entry.rs73
-rw-r--r--lib/domain/libimagwiki/src/error.rs50
-rw-r--r--lib/domain/libimagwiki/src/lib.rs53
-rw-r--r--lib/domain/libimagwiki/src/store.rs96
-rw-r--r--lib/domain/libimagwiki/src/wiki.rs51
7 files changed, 356 insertions, 0 deletions
diff --git a/lib/domain/libimagwiki/Cargo.toml b/lib/domain/libimagwiki/Cargo.toml
new file mode 100644
index 00000000..6d784507
--- /dev/null
+++ b/lib/domain/libimagwiki/Cargo.toml
@@ -0,0 +1,32 @@
+[package]
+name = "libimagwiki"
+version = "0.7.0"
+authors = ["Matthias Beyer <mail@beyermatthias.de>"]
+
+description = "Library for the imag core distribution"
+
+keywords = ["imag", "PIM", "personal", "information", "management"]
+readme = "../../../README.md"
+license = "LGPL-2.1"
+
+documentation = "https://matthiasbeyer.github.io/imag/imag_documentation/index.html"
+repository = "https://github.com/matthiasbeyer/imag"
+homepage = "http://imag-pim.org"
+
+[badges]
+travis-ci = { repository = "matthiasbeyer/imag" }
+is-it-maintained-issue-resolution = { repository = "matthiasbeyer/imag" }
+is-it-maintained-open-issues = { repository = "matthiasbeyer/imag" }
+maintenance = { status = "actively-developed" }
+
+[dependencies]
+error-chain = "0.11"
+toml = "0.4"
+toml-query = "0.6"
+filters = "0.2"
+
+libimagstore = { version = "0.7.0", path = "../../../lib/core/libimagstore" }
+libimagerror = { version = "0.7.0", path = "../../../lib/core/libimagerror" }
+libimagentrylink = { version = "0.7.0", path = "../../../lib/entry/libimagentrylink" }
+libimagentrymarkdown = { version = "0.7.0", path = "../../../lib/entry/libimagentrymarkdown" }
+
diff --git a/lib/domain/libimagwiki/README.md b/lib/domain/libimagwiki/README.md
new file mode 120000
index 00000000..8fe46f2b
--- /dev/null
+++ b/lib/domain/libimagwiki/README.md
@@ -0,0 +1 @@
+../../../doc/src/05100-lib-wiki.md \ No newline at end of file
diff --git a/lib/domain/libimagwiki/src/entry.rs b/lib/domain/libimagwiki/src/entry.rs
new file mode 100644
index 00000000..39b9d805
--- /dev/null
+++ b/lib/domain/libimagwiki/src/entry.rs
@@ -0,0 +1,73 @@
+//
+// imag - the personal information management suite for the commandline
+// Copyright (C) 2015-2018 Matthias Beyer <mail@beyermatthias.de> and contributors
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; version
+// 2.1 of the License.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+//
+
+use libimagstore::store::Store;
+use libimagstore::store::Entry;
+use libimagentrymarkdown::processor::LinkProcessor;
+
+use error::Result;
+use error::WikiErrorKind as WEK;
+use error::ResultExt;
+
+pub trait WikiEntry {
+ fn autolink(&mut self, store: &Store) -> Result<()>;
+ fn autolink_with_processor(&mut self, store: &Store, processor: LinkProcessor) -> Result<()>;
+}
+
+impl WikiEntry for Entry {
+
+ /// Autolink entry to entries linked in content
+ ///
+ /// Uses `libimagentrymarkdown::processor::LinkProcessor` for this, with the following settings:
+ ///
+ /// * Interal link processing = true
+ /// * Internal targets creating = true
+ /// * External link processing = true
+ /// * Processing of Refs = true
+ ///
+ /// This is a convenience function for `WikiEntry::autolink_with_processor()`.
+ ///
+ /// # Warning
+ ///
+ /// With this function, the `LinkProcessor` automatically creates entries in the store if they
+ /// are linked from the current entry but do not exists yet.
+ ///
+ /// # See also
+ ///
+ /// * The documentation of `WikiEntry::autolink_with_processor()`.
+ /// * The documentation of `::libimagentrymarkdown::processor::LinkProcessor`.
+ ///
+ fn autolink(&mut self, store: &Store) -> Result<()> {
+ let processor = LinkProcessor::default()
+ .process_internal_links(true)
+ .create_internal_targets(true)
+ .process_external_links(true)
+ .process_refs(true);
+
+ self.autolink_with_processor(store, processor)
+ }
+
+ /// Autolink entry to entries linked in content with the passed `LinkProcessor` instance.
+ ///
+ /// See the documentation of `::libimagentrymarkdown::processor::LinkProcessor`.
+ fn autolink_with_processor(&mut self, store: &Store, processor: LinkProcessor) -> Result<()> {
+ processor.process(self, store).chain_err(|| WEK::AutoLinkError(self.get_location().clone()))
+ }
+
+}
diff --git a/lib/domain/libimagwiki/src/error.rs b/lib/domain/libimagwiki/src/error.rs
new file mode 100644
index 00000000..0c922a6f
--- /dev/null
+++ b/lib/domain/libimagwiki/src/error.rs
@@ -0,0 +1,50 @@
+//
+// imag - the personal information management suite for the commandline
+// Copyright (C) 2015-2018 Matthias Beyer <mail@beyermatthias.de> and contributors
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; version
+// 2.1 of the License.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+//
+
+use libimagstore::storeid::StoreId;
+
+error_chain! {
+ types {
+ WikiError, WikiErrorKind, ResultExt, Result;
+ }
+
+ links {
+ StoreError(::libimagstore::error::StoreError, ::libimagstore::error::StoreErrorKind);
+ LinkError(::libimagentrylink::error::LinkError, ::libimagentrylink::error::LinkErrorKind);
+ }
+
+ errors {
+ WikiDoesNotExist(name: String) {
+ description("Wiki does not exist")
+ display("Wiki '{}' does not exist", name)
+ }
+
+ WikiExists(name: String) {
+ description("Wiki exist already")
+ display("Wiki '{}' exists already", name)
+ }
+
+ AutoLinkError(sid: StoreId) {
+ description("Error while autolinking entry")
+ display("Error while autolinking entry: {}", sid)
+ }
+ }
+
+}
+
diff --git a/lib/domain/libimagwiki/src/lib.rs b/lib/domain/libimagwiki/src/lib.rs
new file mode 100644
index 00000000..cd6ac333
--- /dev/null
+++ b/lib/domain/libimagwiki/src/lib.rs
@@ -0,0 +1,53 @@
+//
+// imag - the personal information management suite for the commandline
+// Copyright (C) 2015-2018 Matthias Beyer <mail@beyermatthias.de> and contributors
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; version
+// 2.1 of the License.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+//
+
+#![recursion_limit="256"]
+
+#![deny(
+ dead_code,
+ non_camel_case_types,
+ non_snake_case,
+ path_statements,
+ trivial_numeric_casts,
+ unstable_features,
+ unused_allocation,
+ unused_import_braces,
+ unused_imports,
+ unused_must_use,
+ unused_mut,
+ unused_qualifications,
+ while_true,
+)]
+
+extern crate toml;
+extern crate toml_query;
+#[macro_use] extern crate error_chain;
+
+#[macro_use] extern crate libimagstore;
+extern crate libimagerror;
+extern crate libimagentrylink;
+extern crate libimagentrymarkdown;
+
+module_entry_path_mod!("wiki");
+
+pub mod entry;
+pub mod error;
+pub mod store;
+pub mod wiki;
+
diff --git a/lib/domain/libimagwiki/src/store.rs b/lib/domain/libimagwiki/src/store.rs
new file mode 100644
index 00000000..d31b7369
--- /dev/null
+++ b/lib/domain/libimagwiki/src/store.rs
@@ -0,0 +1,96 @@
+//
+// imag - the personal information management suite for the commandline
+// Copyright (C) 2015-2018 Matthias Beyer <mail@beyermatthias.de> and contributors
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; version
+// 2.1 of the License.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+//
+
+use libimagstore::store::Store;
+use libimagstore::storeid::StoreId;
+use libimagstore::storeid::IntoStoreId;
+
+use error::WikiError as WE;
+use error::Result;
+use wiki::Wiki;
+
+pub trait WikiStore {
+
+ fn get_wiki<'a, 'b>(&'a self, name: &'b str) -> Result<Option<Wiki<'a, 'b>>>;
+
+ fn create_wiki<'a, 'b>(&'a self, name: &'b str, mainpagename: Option<&str>)
+ -> Result<Wiki<'a, 'b>>;
+
+ fn retrieve_wiki<'a, 'b>(&'a self, name: &'b str, mainpagename: Option<&str>)
+ -> Result<Wiki<'a, 'b>>;
+
+ fn delete_wiki<N: AsRef<str>>(&self, name: N) -> Result<()>;
+
+}
+
+impl WikiStore for Store {
+
+ /// get a wiki by its name
+ fn get_wiki<'a, 'b>(&'a self, name: &'b str) -> Result<Option<Wiki<'a, 'b>>> {
+ if wiki_path(name.as_ref())?.with_base(self.path().clone()).exists()? {
+ debug!("Building Wiki object");
+ Ok(Some(Wiki::new(self, name)))
+ } else {
+ debug!("Cannot build wiki object: Wiki does not exist");
+ Ok(None)
+ }
+ }
+
+ /// Create a wiki.
+ ///
+ /// # Returns
+ ///
+ /// Returns the Wiki object.
+ ///
+ /// Ob success, an empty Wiki entry with the name `mainpagename` (or "main" if none is passed)
+ /// is created inside the wiki.
+ ///
+ fn create_wiki<'a, 'b>(&'a self, name: &'b str, mainpagename: Option<&str>)
+ -> Result<Wiki<'a, 'b>>
+ {
+ debug!("Trying to get wiki '{}'", name);
+ debug!("Trying to create wiki '{}' with mainpage: '{:?}'", name, mainpagename);
+
+ let wiki = Wiki::new(self, name);
+ wiki.create_entry(mainpagename.unwrap_or("main")).map(|_| wiki)
+ }
+
+ fn retrieve_wiki<'a, 'b>(&'a self, name: &'b str, mainpagename: Option<&str>)
+ -> Result<Wiki<'a, 'b>>
+ {
+ match self.get_wiki(name)? {
+ None => self.create_wiki(name, mainpagename),
+ Some(wiki) => {
+ let _ = wiki.retrieve_entry(mainpagename.unwrap_or("main"))?; // to make sure the page exists
+ Ok(wiki)
+ }
+ }
+ }
+
+ /// Delete a wiki and all entries inside
+ fn delete_wiki<N: AsRef<str>>(&self, name: N) -> Result<()> {
+ unimplemented!()
+ }
+
+}
+
+fn wiki_path(name: &str) -> Result<StoreId> {
+ ::module_path::ModuleEntryPath::new(name).into_storeid().map_err(WE::from)
+}
+
diff --git a/lib/domain/libimagwiki/src/wiki.rs b/lib/domain/libimagwiki/src/wiki.rs
new file mode 100644
index 00000000..24629d74
--- /dev/null
+++ b/lib/domain/libimagwiki/src/wiki.rs
@@ -0,0 +1,51 @@
+//
+// imag - the personal information management suite for the commandline
+// Copyright (C) 2015-2018 Matthias Beyer <mail@beyermatthias.de> and contributors
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; version
+// 2.1 of the License.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+//
+
+use std::path::PathBuf;
+
+use libimagstore::store::Store;
+use libimagstore::store::FileLockEntry;
+use libimagstore::storeid::IntoStoreId;
+
+use error::WikiError as WE;
+use error::Result;
+
+pub struct Wiki<'a, 'b>(&'a Store, &'b str);
+
+impl<'a, 'b> Wiki<'a, 'b> {
+
+ pub(crate) fn new(store: &'a Store, name: &'b str) -> Wiki<'a, 'b> {
+ Wiki(store, name)
+ }
+
+ pub fn create_entry<EN: AsRef<str>>(&self, entry_name: EN) -> Result<FileLockEntry<'a>> {
+ let path = PathBuf::from(format!("{}/{}", self.1, entry_name.as_ref()));
+ let sid = ::module_path::ModuleEntryPath::new(path).into_storeid()?;
+ self.0.create(sid).map_err(WE::from)
+ }
+
+ pub fn retrieve_entry<EN: AsRef<str>>(&self, entry_name: EN) -> Result<FileLockEntry<'a>> {
+ let path = PathBuf::from(format!("{}/{}", self.1, entry_name.as_ref()));
+ let sid = ::module_path::ModuleEntryPath::new(path).into_storeid()?;
+ self.0.retrieve(sid).map_err(WE::from)
+ }
+
+}
+
+