mirror of
https://github.com/CyC2018/Interview-Notebook.git
synced 2026-11-02 20:33:42 +00:00
Preserve Unicode paths in releases and stabilize image manifests (#1476)
This commit is contained in:
@@ -159,7 +159,7 @@ def optimize_images(dist, cache):
|
||||
return before + mapping.get(url, {}).get('path', url) + after
|
||||
parts = re.split(r'(```[^\n]*\n[\s\S]*?```|<!--[\s\S]*?-->)', path.read_text())
|
||||
path.write_text(''.join(part if i % 2 else image_pattern.sub(replace, part) for i, part in enumerate(parts)))
|
||||
(dist / 'image-variants.json').write_text(json.dumps(mapping, ensure_ascii=False, indent=2) + '\n')
|
||||
(dist / 'image-variants.json').write_text(json.dumps(mapping, ensure_ascii=False, indent=2, sort_keys=True) + '\n')
|
||||
return {'converted': len(mapping), 'originalBytes': sum(v['originalBytes'] for v in mapping.values()),
|
||||
'deliveryBytes': sum(v['deliveryBytes'] for v in mapping.values()), 'pixelIdentical': True}
|
||||
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
"""Regression checks for generated search anchors and asset collection."""
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
from collections import Counter
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
||||
from site_assets import heading_slug, referenced_paths, search_document
|
||||
from site_assets import heading_slug, optimize_images, referenced_paths, search_document
|
||||
|
||||
|
||||
class SearchIndexTests(unittest.TestCase):
|
||||
@@ -26,6 +27,22 @@ class SearchIndexTests(unittest.TestCase):
|
||||
content = '<!-- <img src="/secret.png"> -->\n```html\n<img src="/example.png">\n```\n<img src="/notes/pics/actual.png">\n[正文](/notes/示例.md)\n'
|
||||
self.assertEqual(set(referenced_paths(content)), {'notes/pics/actual.png', 'notes/示例.md'})
|
||||
|
||||
def test_image_manifest_stable_across_file_creation_order(self):
|
||||
from PIL import Image
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
root = Path(directory)
|
||||
outputs = []
|
||||
for index, names in enumerate([['a.png', 'z.png'], ['z.png', 'a.png']]):
|
||||
dist = root / str(index)
|
||||
dist.mkdir()
|
||||
for name in names:
|
||||
Image.new('RGB', (128, 128), 'red' if name == 'a.png' else 'blue').save(
|
||||
dist / name, compress_level=0)
|
||||
report = optimize_images(dist, root / 'cache')
|
||||
self.assertEqual(report['converted'], 2)
|
||||
outputs.append((dist / 'image-variants.json').read_bytes())
|
||||
self.assertEqual(outputs[0], outputs[1])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user