1
+ import os
2
+ import sys
3
+ import shutil
4
+
5
+ # open a text file ending with .md and append a paragraph to it
6
+ def reformat_plugin (dirpath , plugin_name ):
7
+ plugins_dir = '/Users/dtyoung/Documents/EEGLAB/sccn.github.io/plugins'
8
+ index_file = os .path .join (plugins_dir , 'index.md' )
9
+ shutil .copyfile (os .path .join (dirpath , 'README.md' ), index_file )
10
+ with open (index_file ) as f :
11
+ text = f .read ()
12
+ append_text = '''---
13
+ layout: default
14
+ title: {plugin_name}
15
+ long_title: {plugin_name}
16
+ parent: Plugins
17
+ ---
18
+ ''' .format (plugin_name = plugin_name )
19
+ text = append_text + text
20
+ with open (index_file , 'w' ) as out :
21
+ out .write (text )
22
+
23
+ # open a text file ending with .md and append a paragraph to it
24
+ # Usage: python test.py <filename>.md
25
+ def append_to_file (filepath , filename , parent , output_file ):
26
+ with open (filepath ) as f :
27
+ text = f .read ()
28
+ append_text = '''---
29
+ layout: default
30
+ title: {filename}
31
+ long_title: {filename}
32
+ parent: {parent}
33
+ grand_parent: Plugins
34
+ ---
35
+ ''' .format (filename = filename , parent = parent )
36
+ text = append_text + text
37
+ with open (output_file , 'w' ) as out :
38
+ out .write (text )
39
+
40
+ def reformat_plugin_dir (plugin_input_dir , plugin_name , plugin_type = 'wiki' ):
41
+ # plugins_output_dir = '/Users/dtyoung/Documents/EEGLAB/sccn.github.io/plugins'
42
+ plugin_output_dir = os .path .join ('/Users/dtyoung/Documents/EEGLAB/sccn.github.io/plugins' , plugin_name )
43
+ if not os .path .exists (plugin_output_dir ):
44
+ os .makedirs (plugin_output_dir )
45
+ # copy image directory from input to output dir
46
+ if os .path .exists (os .path .join (plugin_input_dir , 'images' )):
47
+ shutil .copytree (os .path .join (plugin_input_dir , 'images' ), os .path .join (plugin_output_dir , 'images' ), dirs_exist_ok = True )
48
+
49
+ index_file = os .path .join (plugin_output_dir , 'index.md' )
50
+ if plugin_type == 'wiki' :
51
+ shutil .copyfile (os .path .join (plugin_input_dir , 'Home.md' ), index_file )
52
+ with open (index_file ) as f :
53
+ text = f .read ()
54
+ append_text = '''---
55
+ layout: default
56
+ title: {plugin_name}
57
+ long_title: {plugin_name}
58
+ parent: Plugins
59
+ categories: plugins
60
+ has_children: true
61
+ ---
62
+ ''' .format (plugin_name = plugin_name )
63
+ text = append_text + text
64
+ with open (index_file , 'w' ) as out :
65
+ out .write (text )
66
+
67
+ for root , dirs , files in os .walk (plugin_input_dir ):
68
+ for file in files :
69
+ if file .endswith ('.md' ) and not file .startswith ('index' ) and not file .startswith ('Home' ):
70
+ append_to_file (os .path .join (plugin_input_dir , file ), file .strip ('.md' ), plugin_name , os .path .join (plugin_output_dir , file ))
71
+ else :
72
+ shutil .copyfile (os .path .join (plugin_input_dir , 'README.md' ), index_file )
73
+ with open (index_file ) as f :
74
+ text = f .read ()
75
+ append_text = '''---
76
+ layout: default
77
+ title: {plugin_name}
78
+ long_title: {plugin_name}
79
+ parent: Plugins
80
+ ---
81
+ ''' .format (plugin_name = plugin_name )
82
+ text = append_text + text
83
+ with open (index_file , 'w' ) as out :
84
+ out .write (text )
85
+
86
+ # main
87
+ def main ():
88
+ if len (sys .argv ) != 4 :
89
+ print ('Usage: python test.py <plugin_dir_path> <plugin_name> <plugin_type>' )
90
+ sys .exit (1 )
91
+ dirpath = sys .argv [1 ]
92
+ plugin_name = sys .argv [2 ]
93
+ plugin_type = sys .argv [3 ]
94
+ reformat_plugin_dir (dirpath , plugin_name , plugin_type )
95
+
96
+ if __name__ == "__main__" :
97
+ main ()
0 commit comments