蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

运行 MSWC.NextLink 清单解析、8 项导航方法与 nlcode.inc 边界渲染离线验证

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-contentlinking.py
explore-contentlinking.py199 行
"""Offline Classic ASP Content Linking (MSWC.NextLink) verification lab (IIS ASP absent)."""
import json
from pathlib import Path
import tempfile


class MockNextLink:
    """Offline model of MSWC.NextLink reading tab-delimited Content Linking list files."""

    def __init__(self, current_page: str) -> None:
        self.current_page = current_page

    @staticmethod
    def _load(list_path: Path) -> list[tuple[str, str]]:
        entries: list[tuple[str, str]] = []
        for raw in list_path.read_text(encoding="utf-8").splitlines():
            line = raw.strip()
            if not line:
                continue
            parts = line.split("\t")
            url = parts[0].strip()
            desc = parts[1].strip() if len(parts) > 1 else ""
            entries.append((url, desc))
        return entries

    def GetListCount(self, list_path: Path) -> int:
        return len(self._load(list_path))

    def GetListIndex(self, list_path: Path) -> int:
        entries = self._load(list_path)
        for idx, (url, _) in enumerate(entries, start=1):
            if url.lower() == self.current_page.lower():
                return idx
        return 0

    def GetNthURL(self, list_path: Path, n: int) -> str:
        entries = self._load(list_path)
        if 1 <= n <= len(entries):
            return entries[n - 1][0]
        return ""

    def GetNthDescription(self, list_path: Path, n: int) -> str:
        entries = self._load(list_path)
        if 1 <= n <= len(entries):
            return entries[n - 1][1]
        return ""

    def GetNextURL(self, list_path: Path) -> str:
        entries = self._load(list_path)
        idx = self.GetListIndex(list_path)
        if idx == 0:
            return entries[-1][0]
        if idx < len(entries):
            return entries[idx][0]
        return ""

    def GetNextDescription(self, list_path: Path) -> str:
        entries = self._load(list_path)
        idx = self.GetListIndex(list_path)
        if idx == 0:
            return entries[-1][1]
        if idx < len(entries):
            return entries[idx][1]
        return ""

    def GetPreviousURL(self, list_path: Path) -> str:
        entries = self._load(list_path)
        idx = self.GetListIndex(list_path)
        if idx == 0:
            return entries[0][0]
        if idx > 1:
            return entries[idx - 2][0]
        return ""

    def GetPreviousDescription(self, list_path: Path) -> str:
        entries = self._load(list_path)
        idx = self.GetListIndex(list_path)
        if idx == 0:
            return entries[0][1]
        if idx > 1:
            return entries[idx - 2][1]
        return ""


def render_nav_bar(nl: MockNextLink, list_path: Path) -> str:
    idx = nl.GetListIndex(list_path)
    total = nl.GetListCount(list_path)
    parts = []
    if idx > 1:
        parts.append(f"<a href='{nl.GetPreviousURL(list_path)}'>Previous Page</a>")
    if 0 < idx < total:
        parts.append(f"<a href='{nl.GetNextURL(list_path)}'>Next Page</a>")
    return " ".join(parts)


LINKS_CONTENT = (
    "asp_intro.asp\tASP 简介\n"
    "asp_syntax.asp\tASP 语法\n"
    "asp_variables.asp\tASP 变量\n"
    "asp_procedures.asp\tASP 程序\n"
)


def main() -> None:
    cases = []

    with tempfile.TemporaryDirectory(prefix="asp_nextlink_") as tmp:
        links_file = Path(tmp) / "links.txt"
        links_file.write_text(LINKS_CONTENT, encoding="utf-8")

        # Case 1: GetListCount and GetListIndex (listed vs unlisted)
        nl3 = MockNextLink("asp_variables.asp")
        nl_unlisted = MockNextLink("about.asp")
        expected1 = {"count": 4, "index_variables": 3, "index_unlisted": 0}
        actual1 = {
            "count": nl3.GetListCount(links_file),
            "index_variables": nl3.GetListIndex(links_file),
            "index_unlisted": nl_unlisted.GetListIndex(links_file),
        }
        cases.append({
            "name": "nextlink_count_and_index",
            "summary": "Verify GetListCount=4, GetListIndex=3 for 3rd item, and 0 for unlisted page.",
            "expected": expected1,
            "actual": actual1,
            "passed": actual1 == expected1,
        })

        # Case 2: GetNext* & GetPrevious* for middle page and unlisted fallback
        nl2 = MockNextLink("asp_syntax.asp")
        expected2 = {
            "next_url": "asp_variables.asp",
            "next_desc": "ASP 变量",
            "prev_url": "asp_intro.asp",
            "prev_desc": "ASP 简介",
            "unlisted_next_url": "asp_procedures.asp",
            "unlisted_prev_url": "asp_intro.asp",
        }
        actual2 = {
            "next_url": nl2.GetNextURL(links_file),
            "next_desc": nl2.GetNextDescription(links_file),
            "prev_url": nl2.GetPreviousURL(links_file),
            "prev_desc": nl2.GetPreviousDescription(links_file),
            "unlisted_next_url": nl_unlisted.GetNextURL(links_file),
            "unlisted_prev_url": nl_unlisted.GetPreviousURL(links_file),
        }
        cases.append({
            "name": "nextlink_next_and_previous_navigation",
            "summary": "Verify GetNextURL/Description and GetPreviousURL/Description including unlisted fallback.",
            "expected": expected2,
            "actual": actual2,
            "passed": actual2 == expected2,
        })

        # Case 3: GetNthURL & GetNthDescription
        expected3 = {
            "third_url": "asp_variables.asp",
            "third_desc": "ASP 变量",
            "oob_url": "",
        }
        actual3 = {
            "third_url": nl3.GetNthURL(links_file, 3),
            "third_desc": nl3.GetNthDescription(links_file, 3),
            "oob_url": nl3.GetNthURL(links_file, 99),
        }
        cases.append({
            "name": "nextlink_nth_item_lookup",
            "summary": "Verify GetNthURL and GetNthDescription return 1-based items and empty for out-of-range.",
            "expected": expected3,
            "actual": actual3,
            "passed": actual3 == expected3,
        })

        # Case 4: nlcode.inc navigation bar rendering at first, middle, and last pages
        nav_first = render_nav_bar(MockNextLink("asp_intro.asp"), links_file)
        nav_mid = render_nav_bar(MockNextLink("asp_syntax.asp"), links_file)
        nav_last = render_nav_bar(MockNextLink("asp_procedures.asp"), links_file)
        expected4 = {
            "first": "<a href='asp_syntax.asp'>Next Page</a>",
            "middle": "<a href='asp_intro.asp'>Previous Page</a> <a href='asp_variables.asp'>Next Page</a>",
            "last": "<a href='asp_variables.asp'>Previous Page</a>",
        }
        actual4 = {"first": nav_first, "middle": nav_mid, "last": nav_last}
        cases.append({
            "name": "nextlink_nlcode_inc_nav_render",
            "summary": "Verify nlcode.inc navigation links suppress Previous on page 1 and Next on last page.",
            "expected": expected4,
            "actual": actual4,
            "passed": actual4 == expected4,
        })

    print(json.dumps({
        "version": "CPython 3.12.14 offline Classic ASP / VBScript verifier (IIS ASP absent)",
        "cases": cases,
    }, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。

入口文件:projects/asp-contentlinking-lab/explore-contentlinking.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-contentlinking.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:ASP

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1427.zip

下载课程关联工程包:ASP

课程关联工程包(登记关联36章) · 含当前章节或实例 · mbskycom202612a1425.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习