不使用Apache Server/(.htaccess)运行Xcode DocC

2sbarzqh  于 2023-04-22  发布在  Apache
关注(0)|答案(3)|浏览(133)

在DocC文档"Distributing Documentation to External Developers"中,Apple提供了在您的网站上托管文档存档的文档。不幸的是,当我打开.doccarchive/index.html时,我只看到一个白色页面。他们只显示了Apache服务器的指导。他们指定使用.htaccess文件,并在用户访问文档页面时使用RewriteRule .* SlothCreator.doccarchive/$0 [L]重写URL。
有没有一种方法可以在不运行Apache服务器的情况下打开文档Web应用程序?(我不想进行任何特定于机器的配置,比如修改/etc/hosts)。能够将其作为静态站点托管是理想的(例如在Github页面,Cloudflare页面,Netlify等)。
编辑:使用@Ranoiaetep的answer,我已经构建并将其推送到GitHub repo,可以通过Netlify站点查看:https://xcode-docc.netlify.app/documentation/

jrcvhitl

jrcvhitl1#

到目前为止,我不认为有任何选择托管它作为一个静态网站。
然而,在Netlify上托管它并设置一个.toml文件是非常容易的:

[build]
publish = "ProjectName.doccarchive/"
###### Change it to your doccarchive file's name

[[redirects]]
from = "/documentation/*"
status = 200
to = "/index.html"

[[redirects]]
from = "/tutorials/*"
status = 200
to = "/index.html"

[[redirects]]
from = "/data/documentation.json"
status = 200
to = "/data/documentation/projectname.json"
###### Change it to name in ProjectName.doccarchive/data/documentation/...
# often just all lowercase of your project name

[[redirects]]
force = true
from = "/"
status = 302
to = "/documentation/"

[[redirects]]
force = true
from = "/documentation"
status = 302
to = "/documentation/"

[[redirects]]
force = true
from = "/tutorials"
status = 302
to = "/tutorials/"
cl25kdpy

cl25kdpy2#

现在在SwiftDocCPlugin指南中记录了这一点:

或者,如果您不想在服务器上设置自定义路由规则,或者您在无法设置自定义路由规则的环境中进行托管,则可以生成已转换为静态托管的文档。

  • 具体使用GitHub页面:发布到GitHub Pages。

示例

警告

你会在这个答案中看到一些Unfortunately。我强烈建议你避免使用DocC,原因如下。如果你找到了解决方法,请告诉我:)。在比较方面,DocC与许多成功的静态站点生成器和开源文档框架(Docusaurus)竞争,并且做得不太好。

步骤

  • 将SwiftDocCPlugin添加到Package.swift
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
  • 构建站点,运行:
# Update to your target, from `Package.swift`
TARGET_NAME=SlothCreator
OUTPUT_DIR=docs
swift package --allow-writing-to-directory $OUTPUT_DIR \
    generate-documentation \
    --target $TARGET_NAME \
    --disable-indexing \
    --output-path $OUTPUT_DIR \
    --transform-for-static-hosting
  • cd docs
      • 本地运行**:
  • 您可以启动一个服务器来为您的文件提供服务:运行python3 -m http.server
  • 在浏览器中打开网站:http://localhost:8000/documentation/target_name/
  • 警告:它仍然需要一个web服务器,网站运行得不是很好。如果您访问http://localhost:8000,您会得到一个错误:The page you’re looking for can’t be found.您无法在没有服务器的情况下打开index.html页面。
  • 使用GitHub页面或Cloudflare Workers站点部署它:
  • 当你部署的时候,这一点会被注意到,因为GitHub页面,Cloudflare Workers Sites会为这些页面提供服务。不幸的是,这不适用于Cloudflare Pages,因为它不能构建Swift文档。
    遗憾的是,路径不可配置:/<output-path-specified-by-command-line>/documentation/<target-name>,例如,它可以是:localhost:8000/documentation/slothcreator/
    不幸的是,你必须将你的**生成的
    文档提交到git中。苹果文档中显示了这样的命令:git add docsgit commit -m "Update GitHub pages documentation site."。这是因为Github Pages、Cloudflare Workers Sites等服务无法为您构建网站。
    *不幸的是,此生成的文件夹(docs/)为31MB,其中包含不必要的文件和未优化的大型资产。对于某些服务,例如Cloudflare Workers Sites,您必须在每次发布时上传整个网站。
    *不幸的是,如果你想把它们放在不同的路径,你需要重新生成并单独提交所有文件,因为你需要使用不同的命令。请参阅build.sh中的注解。这意味着它不是31MB,而是N x 31MB,其中N是你拥有的站点数量。
pdkcd3nj

pdkcd3nj3#

Xcode 13.3更新

**注意:**我建议iOS开发者避免使用上面提到的DocC插件,因为如果我们已经安装了Xcode CLI工具,它是一个额外的依赖项。

直到最近Xcode 13.3中的改进才实现了这一点。
我已经在我最新的blog post中概述了通过GitHub Pages部署DocC应用程序/包doccarchive的多个步骤。
解决我遇到的几个问题的关键步骤:
1.确保生成的文档URL和托管的基本路径区分大小写。
1.使用xcodebuild -project ModularSlothCreator.xcodeproj -scheme ModularSlothCreator -parallelizeTargets docbuild构建模块化文档归档。
1.使用docc cli ${xcrun docc} transform-for-static-hosting ..提供的transform-for-static-hosting标志。
有关更多详细信息和CI脚本,请随时参考博客文章。

相关问题