Skip to content

Commit

Permalink
Merge pull request #784 from bokuweb/cacthup-main
Browse files Browse the repository at this point in the history
catch up main
  • Loading branch information
bokuweb authored Nov 15, 2024
2 parents e0bbafa + 960f1b8 commit bc0fa5d
Show file tree
Hide file tree
Showing 280 changed files with 12,004 additions and 11,437 deletions.
86 changes: 43 additions & 43 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,94 +4,94 @@ on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@master
- uses: actions/setup-node@master
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 14
- uses: actions-rs/toolchain@v1
node-version: 20
- uses: moonrepo/setup-rust@v1
with:
profile: minimal
toolchain: stable
override: true
cache: false
inherit-toolchain: true
- name: Cache cargo registry
uses: actions/cache@v1
uses: actions/cache@v4
with:
path: ~/.cargo/registry
key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
- name: Cache cargo index
uses: actions/cache@v1
uses: actions/cache@v4
with:
path: ~/.cargo/git
key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }}
- name: Cache cargo build
uses: actions/cache@v1
uses: actions/cache@v4
with:
path: target
key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('**/Cargo.lock') }}
- name: install deps
run: |
sudo add-apt-repository ppa:libreoffice/ppa
sudo apt-get update
sudo apt-get install libreoffice
npm i -g reg-cli
cd vrt && yarn --frozen-lockfile && cd ../
- name: test
run: make test -B # && make vrt -B
- uses: actions/upload-artifact@master
with:
name: vrt
path: vrt/screenshot

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- uses: actions-rs/toolchain@v1
- uses: actions/checkout@v4
- uses: moonrepo/setup-rust@v1
with:
profile: minimal
toolchain: stable
override: true
inherit-toolchain: true
- name: Cache cargo registry
uses: actions/cache@v1
uses: actions/cache@v4
with:
path: ~/.cargo/registry
key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
- name: Cache cargo index
uses: actions/cache@v1
uses: actions/cache@v4
with:
path: ~/.cargo/git
key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }}
- run: cargo build

build-wasm:
runs-on: ubuntu-latest
permissions:
pull-requests: write
defaults:
run:
working-directory: ./docx-wasm
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@master
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 14
- uses: actions-rs/toolchain@v1
node-version: 20
- uses: moonrepo/setup-rust@v1
with:
profile: minimal
toolchain: 1.58.1
override: true
inherit-toolchain: true
- run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
- run: rustup target add wasm32-unknown-unknown
- run: cd docx-wasm && yarn install && yarn wasm-pack:node && yarn wasm-pack:dev && tsc -p tsconfig.node.json && yarn test
- run: npm install -g pnpm
- run: pnpm install && pnpm wasm-pack:node && pnpm wasm-pack:dev && tsc -p tsconfig.node.json && pnpm test
- name: screenshot
run: pnpm screenshot
- uses: reg-viz/reg-actions@v2
with:
github-token: '${{ secrets.GITHUB_TOKEN }}'
image-directory-path: 'docx-wasm/export-png/png'
threshold-rate: 0.06
enable-antialias: true
matching-threshold: 0.5
artifact-name: wasm

lint:
name: Clippy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: 1.56.1
override: true
- run: rustup component add clippy
- uses: actions-rs/cargo@v1
- uses: actions/checkout@v4
- uses: moonrepo/setup-rust@v1
with:
command: clippy
args: -- -D warnings
inherit-toolchain: true
components: clippy
# TODO(@unvalley): enable
# - run: cargo clippy -- -D warnings
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ Pull requests are always welcome.
2. Create branch: `git checkout -b my-new-feature`
3. Test `docx-core`: `make test && make lint`
4. Update snapshot test result (If needed): `cargo-insta review`
5. Test `docx-wasm`: `cd docx-wasm && yarn install && yarn test`
6. Update snapshot test result (If needed): `cd docx-wasm & yarn test -- --updateSnapshot`
5. Test `docx-wasm`: `cd docx-wasm && pnpm install && pnpm test`
6. Update snapshot test result (If needed): `cd docx-wasm & pnpm test -- --updateSnapshot`
7. Push: `git push origin my-new-feature`
8. Send PR :)

Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ docx-rs = "0.4"
### Browser/Node.js

```
$ yarn add docx-wasm
$ pnpm add docx-wasm
```

## Example
Expand Down Expand Up @@ -91,7 +91,7 @@ writeFileSync("hello.docx", Buffer.from(buffer));
### Requirements

- Node.js 16+
- yarn 1+
- pnpm 9+
- wasm-pack0.10.1 (https://rustwasm.github.io/wasm-pack/)
- insta (https://github.com/mitsuhiko/insta)

Expand Down Expand Up @@ -140,13 +140,13 @@ $ make test
Please run following command.

```
$ cd docx-wasm && yarn install && yarn test
$ cd docx-wasm && pnpm install && pnpm test
```

If snapshot testing is failed, fix code or update snapshot files. (See https://jestjs.io/docs/snapshot-testing).

```
$ yarn test -- --updateSnapshot
$ pnpm test -- --updateSnapshot
```

## Features
Expand Down
7 changes: 4 additions & 3 deletions docx-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "docx-rs"
version = "0.4.17"
version = "0.4.18-rc19"
authors = ["bokuweb <bokuweb12@gmail.com>"]
repository = "https://github.com/bokuweb/docx-rs"
edition = "2018"
Expand All @@ -18,7 +18,8 @@ name = "docx_rs"
path = "src/lib.rs"

[features]
wasm = ["wasm-bindgen", "ts-rs"]
default = ["image"]
wasm = ["wasm-bindgen", "ts-rs", "image"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand All @@ -29,7 +30,7 @@ zip = { version = "0.6.3", default-features = false, features = ["deflate"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = {version = "1.0" }
base64 = "0.13.1"
image = { version = "0.24.4", default-features = false, features=["gif", "jpeg", "png", "bmp", "tiff"] }
image = { version = "0.24.4", default-features = false, features=["gif", "jpeg", "png", "bmp", "tiff"], optional = true }
wasm-bindgen = { version = "0.2.78", optional = true }
ts-rs = { version = "6.1", optional = true }

Expand Down
37 changes: 36 additions & 1 deletion docx-core/src/documents/build_xml.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,38 @@
use crate::xml_builder::XMLBuilder;
use std::io::Write;

pub trait BuildXML {
fn build(&self) -> Vec<u8>;
/// Write XML to the output stream.
#[doc(hidden)]
fn build_to<W: Write>(
&self,
stream: xml::writer::EventWriter<W>,
) -> xml::writer::Result<xml::writer::EventWriter<W>>;

#[doc(hidden)]
fn build(&self) -> Vec<u8> {
self.build_to(XMLBuilder::new(Vec::new()).into_inner().unwrap())
.expect("should write to buf")
.into_inner()
}
}

impl<'a, T: BuildXML> BuildXML for &'a T {
/// Building XML from `&T` is the same as from `T`.
fn build_to<W: Write>(
&self,
stream: xml::writer::EventWriter<W>,
) -> xml::writer::Result<xml::writer::EventWriter<W>> {
(*self).build_to(stream)
}
}

impl<T: BuildXML> BuildXML for Box<T> {
/// Building XML from `Box<T>` is the same as from `T`.
fn build_to<W: Write>(
&self,
stream: xml::writer::EventWriter<W>,
) -> xml::writer::Result<xml::writer::EventWriter<W>> {
(**self).build_to(stream)
}
}
28 changes: 13 additions & 15 deletions docx-core/src/documents/comments.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use super::Comment;
use crate::documents::BuildXML;
use crate::xml_builder::*;
use std::io::Write;

use serde::Serialize;

#[derive(Debug, Clone, PartialEq, Serialize)]
#[derive(Debug, Clone, PartialEq, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct Comments {
pub(crate) comments: Vec<Comment>,
Expand All @@ -28,19 +29,17 @@ impl Comments {
}
}

impl Default for Comments {
fn default() -> Self {
Self { comments: vec![] }
}
}

impl BuildXML for Comments {
fn build(&self) -> Vec<u8> {
let mut b = XMLBuilder::new().declaration(Some(true)).open_comments();
for c in &self.comments {
b = b.add_child(c)
}
b.close().build()
fn build_to<W: Write>(
&self,
stream: xml::writer::EventWriter<W>,
) -> xml::writer::Result<xml::writer::EventWriter<W>> {
XMLBuilder::from(stream)
.declaration(Some(true))?
.open_comments()?
.add_children(&self.comments)?
.close()?
.into_inner()
}
}

Expand All @@ -57,8 +56,7 @@ mod tests {
let b = Comments::new().build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:comments xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" mc:Ignorable="w14 wp14" />"#
r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?><w:comments xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" mc:Ignorable="w14 wp14" />"#
);
}
}
26 changes: 11 additions & 15 deletions docx-core/src/documents/comments_extended.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use serde::Serialize;
use std::io::Write;

use super::*;
use crate::documents::BuildXML;
use crate::xml_builder::*;

// i.e. <w15:commentEx w15:paraId="00000001" w15:paraIdParent="57D1BD7C" w15:done="0"/>
#[derive(Debug, Clone, PartialEq, Serialize)]
#[derive(Debug, Clone, PartialEq, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct CommentsExtended {
pub children: Vec<CommentExtended>,
Expand All @@ -21,21 +22,16 @@ impl CommentsExtended {
}
}

impl Default for CommentsExtended {
fn default() -> Self {
Self { children: vec![] }
}
}

impl BuildXML for CommentsExtended {
fn build(&self) -> Vec<u8> {
let mut b = XMLBuilder::new();
b = b.open_comments_extended();

for c in &self.children {
b = b.add_child(c)
}
b.close().build()
fn build_to<W: Write>(
&self,
stream: xml::writer::EventWriter<W>,
) -> xml::writer::Result<xml::writer::EventWriter<W>> {
XMLBuilder::from(stream)
.open_comments_extended()?
.add_children(&self.children)?
.close()?
.into_inner()
}
}

Expand Down
Loading

0 comments on commit bc0fa5d

Please sign in to comment.