Skip to content

Instantly share code, notes, and snippets.

@mindey
Last active May 21, 2017 12:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mindey/02eff1fcec0098a1fb72 to your computer and use it in GitHub Desktop.
Save mindey/02eff1fcec0098a1fb72 to your computer and use it in GitHub Desktop.

Text Parsing for Sharing

Task description

I have a text-based wiki (a VimWiki) and I want to share arbitrary parts of its texts by automatically copy-pasting the parts marked with {:group1,group2,..,groupN| PART :} into the (shared) folders defined for each group. For example, let's say we have just a single wiki page called OnePage.wiki, and group definitions in GroupDefinitions.wiki.

The task is to recursively copy the extracted and merged fractions of the text into appropriate folders. (Should be a recursive split-apply-merge.) The code you write would be made public, and GPL-licensed.

GroupDefinitions.wiki

{
    "individuals": {
        "friend1": "/media/truecrypt1/vimwikis/friend1",
        "friend2": "/media/truecrypt1/vimwikis/friend2"
    },
    "groups": {
        "group1": ["friend1","friend2"],
        "group2": ["friend2"]
    }
}

Example Input

OnePage.wiki

{:group1|
== Example Story ==
One day, I realized that we could use shared diaries on VIM, and I hacked a solution to let my dear friend also see my diary. We started writing diaries together, side-by-side, every day. We share them via Dropbox, but encrypted, and using gnupg plugin for VimWiki.

It is a wonder to share minds like that together. I think it is like being two hemispheres of brain, connected via corpus callosum. We merged to form something new! Two minds working in unison.

{:-group2|Then. We thought we should share more with our friends, and we found BTSync, which is like Dropbox, but P2P. It was the solution, because we didn't need to teach every friend how to use GPG and VIM. However, there is a little problem that we would like to fix, but have no time right now.:}

We already have a Python script {:-friend1|( https://github.com/Mindey/diary-scripts/blob/master/diary-cron.py ) :}that does something similar. We would like to have a general solution, which goes as deep into the hierarchy defined by nested braces {: :} as needed to parse them.
:}

Example Output

/media/truecrypt1/vimwikis/friend1/OnePage.wiki

== Example Story ==
One day, I realized that we could use shared diaries on VIM, and I hacked a solution to let my dear friend also see my diary. We started writing diaries together, side-by-side, every day. We share them via Dropbox, but encrypted, and using gnupg plugin for VimWiki.

It is a wonder to share minds like that together. I think it is like being two hemispheres of brain, connected via corpus callosum. We merged to form something new! Two minds working in unison.

Then. We thought we should share more with our friends, and we found BTSync, which is like Dropbox, but P2P. It was the solution, because we didn't need to teach every friend how to use GPG and VIM. However, there is a little problem that we would like to fix, but have no time right now.

We already have a Python script that does something similar. We would like to have a general solution, which goes as deep into the hierarchy defined by nested braces {: :} as needed to parse them.

/media/truecrypt1/vimwikis/friend2/OnePage.wiki

== Example Story ==
One day, I realized that we could use shared diaries on VIM, and I hacked a solution to let my dear friend also see my diary. We started writing diaries together, side-by-side, every day. We share them via Dropbox, but encrypted, and using gnupg plugin for VimWiki.

It is a wonder to share minds like that together. I think it is like being two hemispheres of brain, connected via corpus callosum. We merged to form something new! Two minds working in unison.

We already have a Python script ( https://github.com/Mindey/diary-scripts/blob/master/diary-cron.py ) that does something similar. We would like to have a general solution, which goes as deep into the hierarchy defined by nested braces {: :} as needed to parse them.

Hopefully, this solution would let us have a VIM command :Vimwiki2Friends like :Vimwiki2HTML. Unlike the :Vimwiki2HTML, :Vimwiki2HTML would let you automatically distribute the wiki multiple times to different friends by generating different versions from sharing definition.

@Inyuki
Copy link

Inyuki commented Jun 3, 2014

Write a comment :) @mindey

@mindey
Copy link
Author

mindey commented May 17, 2017

By Keziah Wesley:

#!/usr/bin/python3
import json, re

group_defs = json.load(open("GroupDefinitions.wiki", 'r'))
indiv, groups  = group_defs['individuals'], group_defs['groups']
for k, g in groups.items(): groups[k] = frozenset(g)

stack = []
cur = stack
for tok in re.compile('({:|\||:})').split(open("OnePage.wiki", 'r').read()):
    if tok == '{:':   new = []; cur += [new]; stack += [cur]; cur = new
    elif tok == ':}': cur = stack.pop()
    else:             cur += [tok]

out = { k:'' for k in indiv }
def process(stack, group):
    if len(stack) >= 2 and stack[1] == '|':
        if stack[0][0] == '-':
            group = group - groups.get(stack[0][1:], set([stack[0][1:]]))
        else:
            group = group | groups.get(stack[0], set([stack[0]]))
        stack = stack[2:]
    for foo in stack:
        if type(foo) is list:
            process(foo, group)
        else:
            for who in group: out[who] += foo
process(stack, frozenset())

for who, data in out.items():
    open("%s/OnePage.wiki" % (indiv[who],), 'w').write(data)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment