forked from e9t/pelican-loadcsv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloadcsv.py
More file actions
71 lines (52 loc) · 2.78 KB
/
loadcsv.py
File metadata and controls
71 lines (52 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#! /usr/bin/python
# -*- coding: utf-8 -*-
"""
Template CSV loader
-------------------
Authored by Lucy Park <me@lucypark.kr>, 2015
Released under the BSD 2-Clause License (http://opensource.org/licenses/BSD-2-Clause)
"""
import os
import re
import csv
from pelican import signals
def process_col(row):
return [col.replace('\\n', '<br/>') for col in row]
def csv_loader(csv_elem, curpath,\
linefeed='\n', delim=',', classes=['table'], limit=50):
if "'''" in csv_elem:
filename = None
doc = csv_elem.split("'''")[1]
csv_list = doc.split(linefeed)[:limit]
else:
filename = csv_elem.split("'")[1]
filepath = os.path.join('content', curpath, filename)
csv_list = csv.reader(open(filepath, newline=''), delimiter=',', quotechar='"')
if classes:
csv_string = '<table class="%s">' % ' '.join(classes)
else:
csv_string = '<table>'
#csv_string += '<tr><th>%s</th></tr>' % '</th><th>'.join(process_col(csv_list.pop(0).split(delim)))
for row in csv_list:
csv_string += '<tr><td>%s</td></tr>' % '</td><td>'.join(process_col(row))
#if filename:
# csv_string += '<tr><td colspan="%s" class="data-link"><a href="%s">data link</a></td></tr>' % (len(csv_list[0].split(delim)), filename)
csv_string += '</table>'
return csv_string
def loadcsv(data_passed_from_pelican):
"""A function to read through each page and post as it comes through from Pelican, find all instances of triple-backtick (```...```) code blocks, and add an HTML wrapper to each line of each of those code blocks"""
if data_passed_from_pelican._content: # If the item passed from Pelican has a "content" attribute (i.e., if it's not an image file or something else like that)
# NOTE: `data_passed_from_pelican.content` seems to be read-only, whereas `data_passed_from_pelican._content` is able to be overwritten. (Mentioned by Jacob Levernier in his Better Code-Block Line Numbering Plugin)
page_content = data_passed_from_pelican._content
curpath = os.path.dirname(data_passed_from_pelican.get_relative_source_path())
else:
return # Exit the function, essentially passing over the (non-text) file.
all_csv_elements = re.findall('{% csv .*? %}', page_content, re.DOTALL) # re.DOTALL puts python's regular expression engine ('re') into a mode where a dot ('.') matches absolutely anything, including newline characters.
if(len(all_csv_elements) > 0):
updated_page_content = page_content
for csv_elem in all_csv_elements:
replacement = csv_loader(csv_elem, curpath)
updated_page_content = updated_page_content.replace(csv_elem, replacement)
data_passed_from_pelican._content = updated_page_content
def register():
signals.content_object_init.connect(loadcsv)