Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new doc version, doc generation uses xml instead of md, added pygment #4

Merged
merged 9 commits into from
Dec 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ vendor/
.jekyll-metadata
.bundle/
.testcontent/

site/
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ This is the website for [Apache Celix](https://celix.apache.org/), hosted at:
This repository contains:

1. `src/`: the source of the site, including markdown files containing the bulk of the content
1. `site/`: html generated from the markdown (which is what is actually hosted on the website)
1. `build.py`: generates `site/` directory, which contains the celix website
1. `celix/`: the celix sourcecode

### Cloning this repo
Expand Down
132 changes: 73 additions & 59 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@
__license__ = "Apache-2.0"
__version__ = "1.0.0"

import os, inspect, markdown2, shutil, sys
import os, inspect, markdown2, shutil, sys, xml.etree.ElementTree as ET

#define urls
markdown = 'src/markdown'
copy = 'src/noprocess'
snippets = 'src'
out = 'site'
celixIndex = 'celix/documents/intro/readme.md'
ref = {}
docIndex = 'docTableIndex.xml'

#combines two paths to files from a common root directory
#to a path from the parent directory of file one to file two
Expand All @@ -52,66 +51,81 @@ def combinePaths(one, two):
if not os.path.exists(out):
os.makedirs(out)

# read index document
root = ET.parse(docIndex).getroot()
for title in root:
for fl in title:
fl.append(ET.fromstring('<dest>' + out + '/doc/' + (title.attrib['name'] + '/' + fl.attrib['name']).replace(' ', '_') + '.html</dest>'))

# create doctable
docT = '<div id=docTable><table>'
for title in root:
docT += '\n\t<tr><th>' + title.attrib['name'] + '</th></tr>'
for fl in title:
docT += '\n\t<tr><td><a href=/' + fl.find('dest').text[len(out) + 1:] + '>' + fl.attrib['name'] + '</a></td></tr>'
docT = docT.replace('/index.html', '/') + '\n</table></div>\n'

#register site markdown
md = ''
for f in os.listdir(markdown):
ref[markdown + '/' + f] = f[:-3] + '.html'

#read index document
with open(celixIndex) as f:
for line in f:
if(line[0] is '#' and line[1] is '#'):
if(line[2] != '#'):
# declares title
title = line[3:-1].replace(' ', '_')
elif(line[3] != '#'):
# checks if directory exists
if 'title' not in globals():
sys.tracebacklimit = 0
raise NameError("A directory was declared before any title in the index.md file")
if(not os.path.isdir(out + '/doc/' + title)):
os.makedirs(out + '/doc/' + title)
# declares document
key = celixIndex + '/' + line[line.find('(') + 1:line.find(')')]
value = 'doc/' + title + '/' + line[line.find('[') + 1:line.find(']')]
value = value.replace(' ', '_')
ref[key] = value + '.html'
md += '<file name="' + f[:-3] +'"><path>' + markdown + '/' + f + '</path><dest>site/' + f[:-3].replace(' ', '_') + '.html</dest></file>'
root.append(ET.fromstring('<markdown>' + md + '</markdown>'))

# write documents
for key in ref:
try:
f = open(key)
o = open(out + '/' + ref[key], 'w')
except IOError:
print("failed to convert document: \nsource: " + key + "\ndestination: " + out + '/' + ref[key] + "\ncheck if urls are valid, the source file is readable and the destination is creatable.")
continue
file = f.read()

# import other files
i = 0
while True:
i = file.find("{{", i)
if(i == -1):break
i2 = file.find("}}", i)
if(i2 == -1):break
f2 = open(key[:key.rfind('/') + 1] + file[i + 2:i2])
file = file[:i] + f2.read() + file[i2 + 2:]
f2.close()
i = i2 + 2

# correct markdown links
for key2 in ref:
file = file.replace(combinePaths(key, key2), combinePaths(ref[key], ref[key2]))

# convert markdown to html, also add top and bottom html files
file = markdown2.markdown(file, extras=["markdown-in-html"])
with open(snippets + '/top.html') as top:
file = top.read() + file
with open(snippets + '/bottom.html') as bottom:
file = file.encode() + bottom.read()

o.write(file)
f.close()
o.close()
for title in root:
if(title.tag != 'markdown'):
os.makedirs(out + '/doc/' + title.attrib['name'])
for fl in title:
with open(fl.find('path').text) as f:
file = f.read()
o = open(fl.find('dest').text, 'w')

# import images
i = 0
while True:
i = file.find('\n![', i)
if(i == -1):break
i2 = file.find('(', i)
if(i2 == -1):break
i3 = file.find(')', i)
if(i3 == -1):break
if(i2 > i3 or i3 > file.find('\n', i + 3)):
i += 3
continue
# copy images here
with open(fl.find('path').text[:fl.find('path').text.rfind('/') + 1] + file[i2 + 1:i3]) as src:
with open(fl.find('dest').text[:fl.find('dest').text.rfind('/') + 1] + file[i2 + 1:i3], 'w') as dest:dest.write(src.read())
i += 3

# import other files
if fl.find('url'):
i = 0
url = fl.find('url').text.split('url')
while True:
i = file.find(url[0], i)
if(i == -1):break
i2 = file.find(url[1], i)
if(i2 == -1):break
f2 = open(key[:key.rfind('/') + 1] + file[i + len(url[0]):i2])
file = file[:i] + f2.read() + file[i2 + len(url[1]):]
f2.close()
i = i2 + 2

# correct markdown links
for fl2 in root.findall('./title/file'):
file = file.replace(combinePaths(fl.find('path').text,fl2.find('path').text), combinePaths(fl.find('dest').text, fl2.find('dest').text))

# convert markdown to html, also add top and bottom html files
file = markdown2.markdown(file, extras=["markdown-in-html", "break-on-newline", "fenced-code-blocks"])
if(title.tag != 'markdown'):
file = docT + '<div id=docContent>\n' + file + '\n</div>'
with open(snippets + '/top.html') as top:
file = top.read() + file
with open(snippets + '/bottom.html') as bottom:
file = file.encode() + bottom.read()

o.write(file)
o.close()

# run test-server
print('attempting to start a test-server on "http://localhost:8000/"')
Expand Down
28 changes: 28 additions & 0 deletions docTableIndex.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0"?>
<!--
possible arguments:
path: the sourcepath for this file (required)
dest: destination where the path is to be placed (defaults out/doc/titlename/filename.html)
import: configure how a file to be imported is signified in a file. "url" is where the import url should be in the actual file.
mdHtml: set true to parse the document from markdown to html (defaults true)
-->
<root>
<title name="one">
<file name="main">
<path>README.md</path>
<dest>site/doc/index.html</dest>
</file>
<file name="file one">
<path>celix/documents/intro/readme.md</path>
<import>{{url}}</import>
</file>
</title>
<title name="two">
<file name="file two">
<path>celix/documents/getting_started/creating_a_simple_bundle.md</path>
</file>
<file name="file three">
<path>celix/documents/getting_started/readme.md</path>
</file>
</title>
</root>
2 changes: 0 additions & 2 deletions site/404.html

This file was deleted.