def catalog_from_month(year, month): results = [] # 某年对应的目录文件 year_catalog_file_path = tec_constant.TEACHSET_DESPATH( ) + '/' + year + '/' + tec_constant.TEACHSET_MONTH_FILE_NAME() if not os.path.exists(year_catalog_file_path): return results year_catalog_file = open(year_catalog_file_path, 'r') is_find = False for index, line in enumerate(year_catalog_file): # * [1. Swift中lazy作惰性求值](https://github.com/southpeak/iOS-tech-set/blob/master/2017/01.md#Swift中lazy作惰性求值) line = line.strip('\n') if is_find == False: month_regex = r'^(?:##)\s+([0-9]+)' month_match = re.findall(month_regex, line) if month_match: # 是月标题 if month_match[0] == month: is_find = True continue if not is_find: continue # 下个月的标题,结束 if line.startswith('##'): break results.append(line) year_catalog_file.close() return results
def save_catalog(catalog): month = tec_time.current_month() year = tec_time.current_year() # 如果年文件夹不存在需要创建 year_file_path = tec_constant.TEACHSET_DESPATH() + '/' + year if not os.path.exists(year_file_path): os.mkdir(year_file_path) # 某年对应的目录文件 year_catalog_file_path = year_file_path + '/' + tec_constant.TEACHSET_MONTH_FILE_NAME( ) # 年目录文件 if not os.path.exists(year_catalog_file_path): year_catalog_file = open(year_catalog_file_path, 'w') year_catalog_file.write('# ' + year + '目录') year_catalog_file.write('\n') else: year_catalog_file = open(year_catalog_file_path, 'a+') catalog_titles = current_month_catalog_titles() techset_dic = tec_teach_set.tempfile_teachset_titles_authors() if len(catalog_titles) == 0: year_catalog_file.write('\n## ' + month + '\n') index = len(catalog_titles) for (key, teachset_title) in techset_dic.items(): author = key.split('_')[0] is_find = False # 防止重复插入 for catalog in catalog_titles: if teachset_title in catalog: is_find = True break if is_find: print('该标题已经插入目录中,不需要插入:' + teachset_title) continue index += 1 header = '* [%s. %s 【%s】]' % (str(index), teachset_title, author) url_source = 'https://github.com/southpeak/iOS-tech-set/blob/master/%s/%s#' % ( year, month + tec_constant.TEACHSET_FILE_EXTENSION()) encode_title = github_url_encode(teachset_title) catalog_one = '%s(%s%s)' % (header, url_source, encode_title) year_catalog_file.write('\n' + catalog_one)
def write_teach_set_to_file(path): year = tec_time.current_year() month = tec_time.current_month() year_file_path = tec_constant.TEACHSET_DESPATH() + '/' + year month_file_path = year_file_path + '/' + month + tec_constant.TEACHSET_FILE_EXTENSION( ) # 如果年文件夹不存在需要创建 if not os.path.exists(year_file_path): os.mkdir(year_file_path) # 月文件,如果文件已经存在,文件需要写入空行 if os.path.exists(month_file_path): # 检查是否已经写入过小集,需要用标题判断 titles = all_teachset_titles(month_file_path) if len(titles) > 0: month_temp_file = open(path, 'r') lines = month_temp_file.readlines() if len(lines) > 0: f_line = lines[0] if f_line in titles: print("该小集已经添加:" + f_line.strip('\n')) month_temp_file.close() return month_temp_file.close() month_file = open(month_file_path, 'a+') month_file.write('\n\n') else: # 月文件不存在需要,开头写入年月和空行 month_file = open(month_file_path, 'a+') month_file.write('# ' + year + '.' + month) month_file.write('\n') # 遍历小集内容,写入小集中 teactset_file = open(path) for index, line in enumerate(teactset_file): month_file.write(line) month_file.close() teactset_file.close()
def current_month_catalog_titles(): results = set() # 某年对应的目录文件 year_catalog_file_path = tec_constant.TEACHSET_DESPATH( ) + '/' + tec_time.current_year( ) + '/' + tec_constant.TEACHSET_MONTH_FILE_NAME() if not os.path.exists(year_catalog_file_path): return results month = tec_time.current_month() year_catalog_file = open(year_catalog_file_path, 'r') is_find = False for index, line in enumerate(year_catalog_file): # * [1. Swift中lazy作惰性求值](https://github.com/southpeak/iOS-tech-set/blob/master/2017/01.md#Swift中lazy作惰性求值) line = line.strip() if is_find == False: month_regex = r'^(?:##)\s+([0-9]+)' month_match = re.findall(month_regex, line) if month_match: # 是月标题 if month_match[0] == month: is_find = True continue if not is_find: continue # 下个月的标题,结束 if line.startswith('##'): break regex = r'^(?:\*)\s*\[(.{0,})\]' title_match = re.findall(regex, line) for item in title_match: results.add(item) year_catalog_file.close() return results
def update_readme_file(): # README 文件 readme_file_path = tec_constant.TEACHSET_DESPATH( ) + '/' + tec_constant.TEACHSET_README_FILE_NAME() readme_file = open(readme_file_path, 'r') temp_file_path = tec_constant.TEACHSET_DESPATH() + '/' + 'tempreadme.md' temp_file = open(temp_file_path, 'w') locked = False have_update = False for index, line in enumerate(readme_file): if line.startswith('## 感谢'): locked = False if line.startswith('## 最新内容'): locked = True else: if not locked: temp_file.write(line) if locked and not have_update: have_update = True month = tec_time.current_month() year = tec_time.current_year() temp_file.write('## 最新内容') temp_file.write('\n') temp_file.write( '我们会定期整理微博上分享的内容,然后将最新的内容标题放在这里。更多内容可以查看每月的具体内容。\n\n') catalogs = tec_year_catagoy.catalog_from_month(year, month) if len(catalogs) > 0: temp_file.write( '[%s 年 %s 月](https://github.com/southpeak/iOS-tech-set/blob/master/%s/%s)\n' % (year, month, year, month + tec_constant.TEACHSET_FILE_EXTENSION())) for line in catalogs: temp_file.write(line + '\n') if len(catalogs) < 8: # 如果小于 10 条,取上个月的 month_int = datetime.datetime.now().month year_int = datetime.datetime.now().year if month_int == 1: month_str = '12' year_str = str(year_int - 1) else: year_str = str(year_int) if month_int > 9: month_str = str(month_int - 1) else: month_str = '0' + str(month_int - 1) temp_file.write('\n') temp_file.write( '[%s 年 %s 月](https://github.com/southpeak/iOS-tech-set/blob/master/%s/%s)\n' % (year_str, month_str, year_str, month_str + tec_constant.TEACHSET_FILE_EXTENSION())) catalogs = tec_year_catagoy.catalog_from_month( year_str, month_str) for line in catalogs: temp_file.write(line + '\n') # 查看更多 current_year = datetime.datetime.now().year year_path = tec_constant.TEACHSET_DESPATH() + '/' + str( current_year ) + '/' + tec_constant.TEACHSET_MONTH_FILE_NAME() temp_file.write('\n') while (os.path.exists(year_path)): temp_file.write( '[[%s]查看更多 [1] [2] [3] ... ➡️](https://github.com/southpeak/iOS-tech-set/blob/master/%s/%s)\n\n' % (current_year, current_year, tec_constant.TEACHSET_MONTH_FILE_NAME())) current_year -= 1 year_path = tec_constant.TEACHSET_DESPATH() + '/' + str( current_year ) + '/' + tec_constant.TEACHSET_MONTH_FILE_NAME() readme_file.close() temp_file.close() # 重命名,并删除旧的 readme 文件 os.remove(readme_file_path) os.rename(temp_file_path, readme_file_path)