46
46
)
47
47
CMP_IGNORE_FILES = [".git" , ".gitignore" , "README.md" , "build.cfg" ]
48
48
DEVNULL = open (os .devnull , "wb" )
49
- LIST_OF_HUGE_BOOKS : list = ["Installing" , "API reference" ]
50
49
51
50
MASTER_FILE_BASE = "= {title}\n \
52
51
:product-author: {product-author}\n \
@@ -269,19 +268,53 @@ def ensure_directory(directory):
269
268
Creates DIRECTORY if it does not exist.
270
269
"""
271
270
if not os .path .exists (directory ):
272
- os .mkdir (directory )
271
+ os .makedirs (directory )
272
+
273
+ def expand_huge_books (info ):
274
+ """
275
+ Finds nodes for huge books, creates new nodes for books from their top-level topics,
276
+ and then removes the nodes for huge books
277
+ """
278
+
279
+ # find all the huge books, sa defined by nodes
280
+ huge_book_nodes = [book for book in info ["book_nodes" ]
281
+ if os .path .exists (os .path .join (info ["src_dir" ],book ["Dir" ],"hugeBook.flag" )) ]
282
+
283
+
284
+ for book in huge_book_nodes :
285
+ # save the directory in info
286
+ huge_book_dir = book ["Dir" ]
287
+ info ["huge_book_dirs" ].append (huge_book_dir )
288
+ # create the flag file in the book destination directory
289
+ book_dest_dir = os .path .join (info ["dest_dir" ], book ["Dir" ])
290
+ ensure_directory (book_dest_dir )
291
+ with open (os .path .join (book_dest_dir ,"hugeBook.flag" ),"w" ) as fi :
292
+ fi .write ("hugebook" )
293
+ # make new book nodes for the second-level headings
294
+ for topic in book ["Topics" ]:
295
+ if "Dir" in topic .keys ():
296
+ info ["book_nodes" ].append (topic )
297
+ topic ["Dir" ] = huge_book_dir + "/" + topic ["Dir" ]
298
+
299
+ # remove book nodes for huge books
300
+ for node_to_remove in huge_book_nodes :
301
+ info ["book_nodes" ].remove (node_to_remove )
273
302
274
303
275
304
def build_master_files (info ):
276
305
"""
277
306
Builds the master.adoc and docinfo.xml files for each guide specified in the config.
278
307
"""
279
308
309
+ # change the huge books into sub-books
310
+ expand_huge_books (info )
311
+
280
312
# TODO: Refactor. This does too much.
281
313
282
314
dest_dir = info ["dest_dir" ]
283
315
all_in_one = info ["all_in_one" ]
284
316
all_in_one_text = ""
317
+
285
318
for book in info ["book_nodes" ]:
286
319
287
320
book_dest_dir = os .path .join (dest_dir , book ["Dir" ])
@@ -328,40 +361,6 @@ def build_master_files(info):
328
361
info ["preface-title" ] = ":preface-title: " + preface_title
329
362
all_in_one_text += master
330
363
331
- if book ["Name" ] in LIST_OF_HUGE_BOOKS :
332
- huge_book_topics = book ["Topics" ]
333
-
334
- for topic in huge_book_topics :
335
- if "Dir" in topic .keys ():
336
- topic_master_file = os .path .join (
337
- book_dest_dir , topic ["Dir" ], "master.adoc"
338
- )
339
- topic_docinfo_file = os .path .join (
340
- book_dest_dir , topic ["Dir" ], "docinfo.xml"
341
- )
342
-
343
- # TODO: Make less hacky.
344
- book_info ["title" ] = topic ["Name" ]
345
- info ["title" ] = topic ["Name" ]
346
-
347
- master_base = MASTER_FILE_BASE .format (** book_info )
348
- docinfo_node = topic ["Name" ]
349
-
350
- ensure_directory (os .path .join (book_dest_dir , topic ["Dir" ]))
351
- sub_master = generate_master_entry (
352
- topic ,
353
- topic ["Dir" ],
354
- info ["distro" ],
355
- all_in_one ,
356
- all_in_one = all_in_one ,
357
- )
358
-
359
- log .debug ("Writing " + topic_master_file )
360
- with open (topic_master_file , "w" ) as f :
361
- f .write (master_base + sub_master )
362
- log .debug ("Writing " + topic_docinfo_file )
363
- with open (topic_docinfo_file , "w" ) as f :
364
- f .write (DOCINFO_BASE .format (** info ))
365
364
# TODO: And is this ever used?
366
365
if all_in_one :
367
366
master_file = os .path .join (dest_dir , "master.adoc" )
@@ -515,6 +514,7 @@ def copy_file(
515
514
Copies a source file to destination, making sure to scrub the content, add id's where the content is referenced elsewhere and fix any
516
515
links that should be cross references. Also copies any includes that are referenced, since they aren't included in _build_cfg.yml.
517
516
"""
517
+
518
518
# It's possible that the file might have been created by another include, if so then just return
519
519
if os .path .isfile (dest_file ):
520
520
return
@@ -544,15 +544,17 @@ def copy_file(
544
544
key , value = re .split ("\s*=\s*" , meta , 2 )
545
545
include_vars [key ] = value
546
546
547
+
547
548
# Determine the include src/dest paths
548
549
include_file = os .path .join (os .path .dirname (book_src_dir ), include_path )
549
550
relative_path = os .path .relpath (include_file , os .path .dirname (src_file ))
550
551
551
552
# If the path is in another book, copy it into this one
552
553
relative_book_path = os .path .relpath (include_file , book_src_dir )
554
+
553
555
if relative_book_path .startswith ("../" ):
554
- path , src_book_name = os .path .split (book_src_dir )
555
- dest_include_dir = os .path .join (dest_dir , src_book_name , "includes" )
556
+ src_book_relative_dir = os .path .relpath (book_src_dir , info [ "src_dir" ] )
557
+ dest_include_dir = os .path .join (dest_dir , src_book_relative_dir , "includes" )
556
558
relative_path = os .path .join (
557
559
os .path .relpath (dest_include_dir , parent_dir ),
558
560
os .path .basename (include_file ),
@@ -720,7 +722,7 @@ def fix_links(content, info, book_src_dir, src_file, tag=None, cwd=None):
720
722
Fix any links that were done incorrectly and reference the output instead of the source content.
721
723
"""
722
724
if info ["all_in_one" ]:
723
- content = fix_links (content , info ["src_dir" ], src_file , info )
725
+ content = _fix_links (content , info ["src_dir" ], src_file , info )
724
726
else :
725
727
# Determine if the tag should be passed when fixing the links. If it's in the same book, then process the entire file. If it's
726
728
# outside the book then don't process it.
@@ -733,11 +735,27 @@ def fix_links(content, info, book_src_dir, src_file, tag=None, cwd=None):
733
735
734
736
return content
735
737
738
+ def dir_to_book_name (dir ,src_file ,info ):
739
+ # find a book name by the directory
740
+ for book in info ["book_nodes" ]:
741
+ if book ["Dir" ] == dir :
742
+ return (book ["Name" ])
743
+ break
744
+
745
+ has_errors = True
746
+ log .error (
747
+ 'ERROR (%s): book not found for the directory %s' ,
748
+ src_file ,
749
+ dir )
750
+ return (dir )
751
+
736
752
737
753
def _fix_links (content , book_dir , src_file , info , tag = None , cwd = None ):
738
754
"""
739
755
Fix any links that were done incorrectly and reference the output instead of the source content.
740
756
"""
757
+ current_book_name = dir_to_book_name (os .path .relpath (book_dir ,info ["src_dir" ]),src_file ,info )
758
+
741
759
# TODO Deal with xref so that they keep the proper path. Atm it'll just strip the path and leave only the id
742
760
file_to_id_map = info ["file_to_id_map" ]
743
761
current_dir = cwd or os .path .dirname (src_file )
@@ -750,39 +768,56 @@ def _fix_links(content, book_dir, src_file, info, tag=None, cwd=None):
750
768
link_anchor = link .group (2 )
751
769
link_title = link .group (3 )
752
770
771
+ # sanity check - is this a link to an external site?
772
+ # apparently the link macro CAN be used for internal links too, so just testing for http
773
+ # NOTE: a docs.openshift.com link would not process here corectly, anyway, so let it pass through
774
+ if ("http:" in link_text ) or ("https:" in link_text ):
775
+ continue
776
+
777
+ fixed_link = "" # setting the scope of fixed_link outside the if statements
778
+
753
779
if link_file is not None :
754
780
fixed_link_file = link_file .replace (".html" , ".adoc" )
755
781
fixed_link_file_abs = os .path .abspath (
756
782
os .path .join (current_dir , fixed_link_file )
757
783
)
758
784
if fixed_link_file_abs in file_to_id_map :
759
785
760
- # We are dealing with a cross reference to another book here
761
- external_link = EXTERNAL_LINK_RE .search (link_file )
762
- book_dir_name = external_link .group (1 )
786
+ # We are dealing with a cross reference to a book here
787
+ full_relative_path = os .path .relpath (fixed_link_file_abs ,info ["src_dir" ])
788
+
789
+ if full_relative_path [:2 ]== ".." :
790
+ log .error (
791
+ 'ERROR (%s): link pointing outside source directory? %s' ,
792
+ src_file ,
793
+ link_file )
794
+ continue
795
+ split_relative_path = full_relative_path .split ("/" )
796
+ book_dir_name = split_relative_path [0 ]
797
+ if book_dir_name in info ["huge_book_dirs" ]:
798
+ book_dir_name = split_relative_path [0 ]+ "/" + split_relative_path [1 ]
763
799
764
800
# Find the book name
765
- book_name = book_dir_name
766
- for book in info ["data" ]:
767
- if (
768
- check_node_distro_matches (book , info ["distro" ])
769
- and book ["Dir" ] == book_dir_name
770
- ):
771
- book_name = book ["Name" ]
772
- break
773
-
774
- fixed_link_file = BASE_PORTAL_URL + build_portal_url (info , book_name )
775
-
776
- if link_anchor is None :
777
- fixed_link = (
778
- "link:"
779
- + fixed_link_file
780
- + "#"
781
- + file_to_id_map [fixed_link_file_abs ]
782
- + link_title
783
- )
801
+ book_name = dir_to_book_name (book_dir_name ,src_file ,info )
802
+
803
+
804
+ if book_name == current_book_name :
805
+ if link_anchor is None :
806
+ fixed_link = "xref:" + file_to_id_map [fixed_link_file_abs ] + link_title
807
+ else :
808
+ fixed_link = "xref:" + link_anchor .replace ("#" , "" ) + link_title
784
809
else :
785
- fixed_link = "link:" + fixed_link_file + link_anchor + link_title
810
+ fixed_link_file = BASE_PORTAL_URL + build_portal_url (info , book_name )
811
+ if link_anchor is None :
812
+ fixed_link = (
813
+ "link:"
814
+ + fixed_link_file
815
+ + "#"
816
+ + file_to_id_map [fixed_link_file_abs ]
817
+ + link_title
818
+ )
819
+ else :
820
+ fixed_link = "link:" + fixed_link_file + link_anchor + link_title
786
821
else :
787
822
# Cross reference or link that isn't in the docs suite
788
823
fixed_link = link_text
@@ -1132,6 +1167,7 @@ def main():
1132
1167
"all_in_one" : args .all_in_one ,
1133
1168
"preface-title" : "" ,
1134
1169
"upstream_branch" : args .upstream_branch ,
1170
+ "huge_book_dirs" : []
1135
1171
}
1136
1172
1137
1173
# Build the master files
0 commit comments