import re
import os
import zipfile
import xml.etree.ElementTree as ET
def is_newline(elem):
return elem.tag == '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}pPr'
def is_codesnippet_end(attr):
return attr == 'Para' or attr == 'FeatureType'
def convert_title_to_directory_name(raw_header):
# Break title into component words
words_array = re.split(r'[\s|-]+', raw_header)
# Strip special characters
words_array = list(map(lambda x: ''.join(
e for e in x if e.isalnum()), words_array))
# Remove empty strings
while('' in words_array):
words_array.remove('')
# Capitalize each word
words_array = map(lambda x: x.capitalize()
if not x[0].istitle() else x, words_array)
return ''.join(words_array)
def get_directory_path_from_stack(stack):
return '/'.join(map(lambda x: x['title'], stack))
def get_full_file_path_from_stack(stack, file_extension):
path = get_directory_path_from_stack(stack)
top_of_stack = stack[-1]
# 0-padded number
next_file_number = str(top_of_stack['next_index'])
if len(next_file_number) == 1:
next_file_number = '0' + next_file_number
top_of_stack['next_index'] += 1
return f'''{path}/{top_of_stack['title']}Example{next_file_number}.{file_extension}'''
def guess_file_extension(buffer):
html_snippets = (
'<html>',
'<script',
'<div>',
'<span>',
'<p>',
)
for snippet in html_snippets:
if snippet in buffer:
return 'html'
return 'js'
DOCX_FILES = [f'''src/c{'0' if x < 10 else ''}{x}.docx''' for x in range(
1, 29)] + [f'src/appendix{x}.docx' for x in list('ABCD')]
for filename in DOCX_FILES:
xml_directory = filename.split('.')[0]
with zipfile.ZipFile(filename, 'r') as zip_ref:
zip_ref.extractall(xml_directory)
tree = ET.parse(f'{xml_directory}/word/document.xml')
root = tree.getroot()
header_text_buffer = ''
code_text_buffer = ''
inside_header_tag = False
inside_code_snippet = False
expected_stack_height = 0
stack = []
for elem in root.iter():
attr = None
try:
attr = elem.attrib['{http://schemas.openxmlformats.org/wordprocessingml/2006/main}val']
except Exception as e:
pass
if attr == 'H1' or attr == 'H2' or attr == 'H3' or attr == 'H4' or attr == 'ChapterTitle' or attr == 'AppendixTitle':
inside_header_tag = True
inside_code_snippet = False
if attr == 'ChapterTitle' or attr == 'AppendixTitle':
expected_stack_height = 1
elif attr == 'H1':
expected_stack_height = 2
elif attr == 'H2':
expected_stack_height = 3
elif attr == 'H3':
expected_stack_height = 4
elif attr == 'H4':
expected_stack_height = 5
if attr == 'CodeSnippet':
inside_header_tag = False
inside_code_snippet = True
if inside_header_tag and is_newline(elem):
inside_header_tag = False
title = convert_title_to_directory_name(header_text_buffer)
if expected_stack_height == len(stack) + 1:
pass
elif expected_stack_height == len(stack):
stack.pop()
elif expected_stack_height < len(stack):
while expected_stack_height <= len(stack):
stack.pop()
else:
raise 'This should never happen!'
stack.append({
'title': title,
'next_index': 1
})
header_text_buffer = ''
code_text_buffer = ''
if inside_code_snippet and is_codesnippet_end(attr):
inside_code_snippet = False
try:
os.makedirs(get_directory_path_from_stack(stack))
except Exception as e:
pass
file_extension = guess_file_extension(code_text_buffer)
f = open(get_full_file_path_from_stack(stack, file_extension), "a")
f.write(code_text_buffer)
f.close()
header_text_buffer = ''
code_text_buffer = ''
if inside_header_tag:
if elem.text is not None:
header_text_buffer += elem.text
if inside_code_snippet:
if elem.text is not None:
code_text_buffer += elem.text
elif is_newline(elem):
code_text_buffer += '\n'
没有合适的资源?快使用搜索试试~ 我知道了~
JavaScript高级程序设计第四版-示例代码
共1919个文件
js:1819个
html:96个
py:1个
需积分: 5 7 下载量 27 浏览量
2023-05-22
10:43:00
上传
评论
收藏 2.93MB ZIP 举报
温馨提示
JavaScript高级程序设计第四版--示例代码
资源推荐
资源详情
资源评论
收起资源包目录
JavaScript高级程序设计第四版-示例代码 (1919个子文件)
.gitignore 12B
ComputedStylesExample01.html 1KB
PromiseCancelingExample02.html 987B
AutomaticTabForwardExample02.html 774B
AddingWebComponentContentExample02.html 686B
NodeIteratorExample06.html 676B
ConditionalDeclarationExample02.html 634B
LoggingMessagesToThePageExample01.html 633B
MultiplexingAMutationObserverExample01.html 584B
TheContextmenuEventExample01.html 534B
UsingAShadowDOMExample02.html 522B
AddingWebComponentContentExample01.html 477B
ConditionalDeclarationExample01.html 475B
TemplateScriptsExample01.html 466B
UsingTemplateTagsExample01.html 465B
TheQueryselectorallMethodExample01.html 397B
CompositionAndShadowDOMSlotsExample05.html 381B
UsingAShadowDOMExample03.html 372B
TreeWalkerExample02.html 362B
RemovingEventHandlersExample02.html 356B
UsingAShadowDOMExample01.html 315B
CreatingShaderProgramsExample01.html 315B
RemovingEventHandlersExample01.html 314B
DocumentWritingExample04.html 301B
TheNoscriptElementExample01.html 299B
CompositionAndShadowDOMSlotsExample04.html 290B
TheXSLTProcessorTypeExample03.html 290B
ManipulatingTheLocationExample04.html 286B
BlobsAndPartialReadsExample01.html 283B
ModuleTaggingAndDefinitionExample04.html 274B
ChangesInXHTMLExample04.html 272B
IntroductionToShadowDOMExample02.html 270B
ChangesInXHTMLExample03.html 266B
DecoupleHTMLJavaScriptExample01.html 257B
ChangesInXHTMLExample02.html 256B
DecoupleHTMLJavaScriptExample02.html 255B
ChangesInXHTMLExample01.html 253B
DocumentWritingExample01.html 247B
UsingInnerhtmlInLegacyInternetExplorerExample02.html 232B
DocumentWritingExample03.html 230B
DocumentWritingExample02.html 229B
CompositionAndShadowDOMSlotsExample02.html 225B
AsynchronousScriptsExample01.html 216B
DeferredScriptsExample01.html 216B
TraversalsExample01.html 216B
TagPlacementExample02.html 204B
TagPlacementExample01.html 204B
RichTextEditingExample02.html 201B
TheTextTypeExample01.html 198B
RelatedElementsExample01.html 191B
ModuleTaggingAndDefinitionExample03.html 181B
EntryPointsExample01.html 175B
TheInnertextPropertyExample01.html 173B
TheOuterhtmlPropertyExample01.html 173B
TheInnerhtmlPropertyExample01.html 173B
UsingADocumentFragmentExample03.html 167B
DocumentChildrenExample02.html 166B
BackwardsCompatibilityExample01.html 159B
ModuleTaggingAndDefinitionExample02.html 151B
InlineCodeVersusExternalFilesExample01.html 151B
InlineCodeVersusExternalFilesExample02.html 151B
HTMLEventHandlersExample03.html 148B
CompositionAndShadowDOMSlotsExample01.html 144B
TheUnloadEventExample02.html 142B
NodeIteratorExample05.html 142B
TheLoadEventExample02.html 136B
TheInnerhtmlPropertyExample02.html 135B
EventBubblingExample01.html 134B
TheDocumentObjectModelExample01.html 124B
RichTextEditingExample01.html 121B
ModuleTaggingAndDefinitionExample01.html 109B
IntroductionToShadowDOMExample01.html 108B
HierarchyOfNodesExample01.html 108B
CompositionAndShadowDOMSlotsExample03.html 106B
DocumentChildrenExample05.html 93B
SimpleSelectionInDOMRangesExample01.html 89B
UsingADocumentFragmentExample01.html 87B
TheScriptElementExample03.html 81B
LocatingElementsExample02.html 80B
TheScriptElementExample02.html 80B
DeprecatedSyntaxExample01.html 76B
TemplateLiteralsExample02.html 76B
InsertingDOMRangeContentExample04.html 72B
UsingInnerhtmlInLegacyInternetExplorerExample01.html 70B
TheScriptElementExample01.html 68B
UnknownRuntimeErrorExample01.html 66B
DynamicScriptsExample05.html 61B
TheScriptElementExample06.html 58B
TheOuterhtmlPropertyExample02.html 47B
DocumentChildrenExample01.html 46B
InteractingWithDOMRangeContentExample07.html 41B
TheScriptElementExample04.html 35B
InteractingWithDOMRangeContentExample05.html 35B
InteractingWithDOMRangeContentExample01.html 34B
DynamicScriptsExample01.html 31B
TheScriptElementExample05.html 28B
InteractingWithDOMRangeContentExample03.html 21B
EventReferenceExample01.js 6KB
HowPrototypesWorkExample01.js 2KB
StreamDecodingExample01.js 2KB
共 1919 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
隐形喷火龙
- 粉丝: 205
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功