emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[O] Publishing Org to Org?


From: François Pinard
Subject: [O] Publishing Org to Org?
Date: Sun, 03 Jun 2012 21:58:45 -0400

Hi, Org people!

GitHub renders a README.md file right at the end of the project page,
using bigger fonts for titles, producing clickable links, and such
things.  As I did not feel like maintaining the same information both in
Org and Markdown format, I used the Org -> [org-publish] -> HTML ->
[Pandoc] -> Md route.  I do not make the Org file directly available, as
there are not wholly meant for publication.  It works well because the
non-exportable and other private bits are stripped before the HTML is
produced.  However, Org tables do not nicely survive this route.

Now, GitHub allows for README.org files, and renders them.  The
rendering job is far from perfect, but still, it might be acceptable if
one limits himself to those Org features which GitHub processes
correctly.  One good thing is that we get better tables this way.  I may
not directly publish the original Org file because of its private parts.
So I quickly wrote a small Org copier, tied to my own habits and not
trying to be generic.  I provide it below, in case someone is curious.

My feeling is that ideally, Org should itself provide a standard Org
exporter, as generic as it should be.  One tiny problem, among all
others, is to decide which file extension to use for the result, as .org
is already used for the input file.  Just a thought :-).

François

--8<---------------cut here---------------start------------->8---
#!/usr/bin/env python3

"""\
Trim the private parts of an Org file.

Usage: trim-org [ORG_FILE]

Results go to standard output.
"""

import re, sys

header_prefix_regexp = ('^(\\*+) '
                        '(\\[#[ABC]\\] )?'
                        '(TODO |NEXT |DONE |WAIT )?'
                        '(\\*)?')
# 1 = stars, 2 = priority, 3 = keyword, 4 = once
# 5 = text followed by spaces, 6 = tags
header_regexp = header_prefix_regexp + '(.*?)((:[a-z]+)+:)?$'

class Main:

    def main(self, *arguments):
        import getopt
        options, arguments = getopt.getopt(arguments, '')
        for option, value in options:
            pass
        if arguments:
            for argument in arguments:
                self.trim_file(open(argument), sys.stdout.write)
        else:
            self.trim_file(sys.stdin, sys.stdout.write)

    def trim_file(self, lines, write):
        noexport = None
        for line in lines:
            match = re.match(header_regexp, line)
            if match is not None:
                level = len(match.group(1))
                if noexport is not None and level <= noexport:
                    noexport = None
                tags = match.group(6)
                if noexport is None and tags and ':noexport:' in tags:
                    noexport = level
            if noexport is None:
                if (not line.startswith('#')
                        or line.startswith('#+')
                        or line.startswith('# <<')):
                    write(line)

run = Main()
main = run.main

if __name__ == '__main__':
    main(*sys.argv[1:])
--8<---------------cut here---------------end--------------->8---
























reply via email to

[Prev in Thread] Current Thread [Next in Thread]