diff --git a/src/.gitignore b/src/.gitignore new file mode 100644 index 0000000..eb5a316 --- /dev/null +++ b/src/.gitignore @@ -0,0 +1 @@ +target diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..a26bfb6 --- /dev/null +++ b/src/Makefile @@ -0,0 +1,102 @@ +ANDROID_OUT=../android/src/main/jniLibs +ANDROID_SDK=$(HOME)/Library/Android/sdk +NDK_BIN=$(ANDROID_SDK)/ndk/23.1.7779620/toolchains/llvm/prebuilt/darwin-x86_64/bin +LIB_NAME=libreadability + +android-armv7a: + CGO_ENABLED=1 \ + GOOS=android \ + GOARCH=arm \ + GOARM=7 \ + CC=$(NDK_BIN)/armv7a-linux-androideabi21-clang \ + go build -buildmode=c-shared -o $(ANDROID_OUT)/armeabi-v7a/${LIB_NAME}.so . + +android-arm64: + CGO_ENABLED=1 \ + GOOS=android \ + GOARCH=arm64 \ + CC=$(NDK_BIN)/aarch64-linux-android21-clang \ + go build -buildmode=c-shared -o $(ANDROID_OUT)/arm64-v8a/${LIB_NAME}.so . + +android-x86: + CGO_ENABLED=1 \ + GOOS=android \ + GOARCH=386 \ + CC=$(NDK_BIN)/i686-linux-android21-clang \ + go build -buildmode=c-shared -o $(ANDROID_OUT)/x86/${LIB_NAME}.so . + +android-x86_64: + CGO_ENABLED=1 \ + GOOS=android \ + GOARCH=amd64 \ + CC=$(NDK_BIN)/x86_64-linux-android21-clang \ + go build -buildmode=c-shared -o $(ANDROID_OUT)/x86_64/${LIB_NAME}.so . + +ios-x86_64-sim: + GOARCH=amd64 \ + SDK=iphonesimulator \ + LIB_NAME=${LIB_NAME} \ + ./build_ios.sh + +ios-arm64-sim: + GOARCH=arm64 \ + SDK=iphonesimulator \ + LIB_NAME=${LIB_NAME} \ + ./build_ios.sh + +ios-arm64: + GOARCH=arm64 \ + SDK=iphoneos \ + LIB_NAME=${LIB_NAME} \ + ./build_ios.sh + +android: android-armv7a android-arm64 android-x86 android-x86_64 + +ios: ios-x86_64-sim ios-arm64-sim ios-arm64 + lipo \ + -create \ + ${LIB_NAME}_arm64_iphonesimulator.a \ + ${LIB_NAME}_amd64_iphonesimulator.a \ + -output ${LIB_NAME}_iphonesimulator.a + rm ${LIB_NAME}_arm64_iphonesimulator.* + rm ${LIB_NAME}_amd64_iphonesimulator.* + + mkdir -p ios-arm64 + mkdir -p ios-simulator + mv ./${LIB_NAME}_arm64_iphoneos.a ./ios-arm64/${LIB_NAME}.a + cp ./${LIB_NAME}_arm64_iphoneos.h ./ios-arm64/${LIB_NAME}.h + mv ./${LIB_NAME}_iphonesimulator.a ./ios-simulator/${LIB_NAME}.a + mv ./${LIB_NAME}_arm64_iphoneos.h ./ios-simulator/${LIB_NAME}.h + + xcodebuild -create-xcframework \ + -output ${LIB_NAME}.xcframework \ + -library ios-arm64/${LIB_NAME}.a \ + -headers ios-arm64/${LIB_NAME}.h \ + -library ios-simulator/${LIB_NAME}.a \ + -headers ios-simulator/${LIB_NAME}.h + rm -rf ios-arm64 + rm -rf ios-arm64-simulator + rm -rf ios-simulator + rm -rf ../ios/${LIB_NAME}.xcframework + mv ${LIB_NAME}.xcframework ../ios/ + +macos: + CGO_ENABLED=1 \ + GOOS=darwin \ + GOARCH=arm64 \ + SDK=macos \ + go build -trimpath -buildmode=c-shared -o ${LIB_NAME}_arm64.dylib . + CGO_ENABLED=1 \ + GOOS=darwin \ + GOARCH=amd64 \ + SDK=macos \ + go build -trimpath -buildmode=c-shared -o ${LIB_NAME}_amd64.dylib . + lipo \ + -create \ + ${LIB_NAME}_arm64.dylib \ + ${LIB_NAME}_amd64.dylib \ + -output ${LIB_NAME}.dylib + install_name_tool -id "@rpath/libreadability.dylib" libreadability.dylib + mv ${LIB_NAME}.dylib ../macos/${LIB_NAME}.dylib + rm ${LIB_NAME}_*.h + rm ${LIB_NAME}_*.dylib \ No newline at end of file diff --git a/src/android.sh b/src/android.sh new file mode 100755 index 0000000..7284aa8 --- /dev/null +++ b/src/android.sh @@ -0,0 +1,15 @@ +#!/usr/bin/sh +export NDK_PATH=/home/sphericalkat/Android/Sdk/ndk/27.0.11902837 +export TARGET=aarch64-linux-android +export API=21 + +export CC=$NDK_PATH/toolchains/llvm/prebuilt/linux-x86_64/bin/$TARGET$API-clang +export STRIP=$NDK_PATH/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-strip +export CXX=$NDK_PATH/toolchains/llvm/prebuilt/linux-x86_64/bin/$TARGET$API-clang++ +export CGO_CFLAGS="-I$NDK_PATH/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include" +export CGO_CPPFLAGS="-I$NDK_PATH/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include" +export CGO_CXXFLAGS="-I$NDK_PATH/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include" +export CGO_LDFLAGS="-L$NDK_PATH/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib" + +CGO_ENABLED=1 GOOS=android GOARCH=arm64 go build -o target/android-arm64/libreadability.so -buildmode=c-shared main.go +$STRIP target/android-arm64/libreadability.so \ No newline at end of file diff --git a/src/build_ios.sh b/src/build_ios.sh new file mode 100755 index 0000000..f7c3c90 --- /dev/null +++ b/src/build_ios.sh @@ -0,0 +1,25 @@ +#!/bin/sh +export GOOS=ios +export CGO_ENABLED=1 +# export CGO_CFLAGS="-fembed-bitcode" +# export MIN_VERSION=15 + +SDK_PATH=$(xcrun --sdk "$SDK" --show-sdk-path) + +if [ "$GOARCH" = "amd64" ]; then + CARCH="x86_64" +elif [ "$GOARCH" = "arm64" ]; then + CARCH="arm64" +fi + +if [ "$SDK" = "iphoneos" ]; then + export TARGET="$CARCH-apple-ios$MIN_VERSION" +elif [ "$SDK" = "iphonesimulator" ]; then + export TARGET="$CARCH-apple-ios$MIN_VERSION-simulator" +fi + +CLANG=$(xcrun --sdk "$SDK" --find clang) +CC="$CLANG -target $TARGET -isysroot $SDK_PATH $@" +export CC + +go build -trimpath -buildmode=c-archive -o ${LIB_NAME}_${GOARCH}_${SDK}.a \ No newline at end of file diff --git a/src/go.mod b/src/go.mod new file mode 100644 index 0000000..98373ae --- /dev/null +++ b/src/go.mod @@ -0,0 +1,13 @@ +module github.com/SphericalKat/readability-go + +go 1.22.2 + +require ( + github.com/andybalholm/cascadia v1.3.2 // indirect + github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de // indirect + github.com/go-shiori/dom v0.0.0-20230515143342-73569d674e1c // indirect + github.com/go-shiori/go-readability v0.0.0-20240701094332-1070de7e32ef // indirect + github.com/gogs/chardet v0.0.0-20211120154057-b7413eaefb8f // indirect + golang.org/x/net v0.27.0 // indirect + golang.org/x/text v0.16.0 // indirect +) diff --git a/src/go.sum b/src/go.sum new file mode 100644 index 0000000..cae21c4 --- /dev/null +++ b/src/go.sum @@ -0,0 +1,57 @@ +github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsViSLyss= +github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6lUvCFb+h7KvU= +github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de h1:FxWPpzIjnTlhPwqqXc4/vE0f7GvRjuAsbW+HOIe8KnA= +github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de/go.mod h1:DCaWoUhZrYW9p1lxo/cm8EmUOOzAPSEZNGF2DK1dJgw= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-shiori/dom v0.0.0-20230515143342-73569d674e1c h1:wpkoddUomPfHiOziHZixGO5ZBS73cKqVzZipfrLmO1w= +github.com/go-shiori/dom v0.0.0-20230515143342-73569d674e1c/go.mod h1:oVDCh3qjJMLVUSILBRwrm+Bc6RNXGZYtoh9xdvf1ffM= +github.com/go-shiori/go-readability v0.0.0-20240701094332-1070de7e32ef h1:6y2GmHDeuF2xwC5L7fLMTlgnOjm5Jy8RYDI1YYpcOKU= +github.com/go-shiori/go-readability v0.0.0-20240701094332-1070de7e32ef/go.mod h1:jH+l/xV/8x8utphLx72GLIuw9wGhGzrZS5i7arOk8zc= +github.com/gogs/chardet v0.0.0-20211120154057-b7413eaefb8f h1:3BSP1Tbs2djlpprl7wCLuiqMaUh5SJkkzI2gDs+FgLs= +github.com/gogs/chardet v0.0.0-20211120154057-b7413eaefb8f/go.mod h1:Pcatq5tYkCW2Q6yrR2VRHlbHpZ/R4/7qyL1TCF7vl14= +github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/scylladb/termtables v0.0.0-20191203121021-c4c0b6d42ff4/go.mod h1:C1a7PQSMz9NShzorzCiG2fk9+xuCgLkPeCvMHYR2OWg= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= +golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns= +golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys= +golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= +golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= +golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/src/main.go b/src/main.go new file mode 100644 index 0000000..3fad63a --- /dev/null +++ b/src/main.go @@ -0,0 +1,95 @@ +package main + +/* +#include + +typedef struct { + char* title; + char* author; + int length; + char* excerpt; + char* site_name; + char* image_url; + char* favicon_url; + char* content; // HTML content + char* text_content; // text content + char* language; + char* published_time; + char* err; + int success; +} CArticle; +*/ +import "C" +import ( + "time" + "unsafe" + + "github.com/go-shiori/go-readability" +) + +//export Parse +func Parse(url *C.char) (result C.CArticle) { + goURL := C.GoString(url) + article, err := readability.FromURL(goURL, 30*time.Second) + if err != nil { + return C.CArticle{ + err: C.CString(err.Error()), + success: 0, + } + } + + return C.CArticle{ + title: C.CString(article.Title), + author: C.CString(article.Byline), + length: C.int(article.Length), + excerpt: C.CString(article.Excerpt), + site_name: C.CString(article.SiteName), + image_url: C.CString(article.Image), + favicon_url: C.CString(article.Favicon), + content: C.CString(article.Content), + text_content: C.CString(article.TextContent), + err: nil, + success: 1, + } +} + +//export FreeArticle +func FreeArticle(article C.CArticle) { + if article.title != nil { + C.free(unsafe.Pointer(article.title)) + } + if article.author != nil { + C.free(unsafe.Pointer(article.author)) + } + if article.excerpt != nil { + C.free(unsafe.Pointer(article.excerpt)) + } + if article.site_name != nil { + C.free(unsafe.Pointer(article.site_name)) + } + if article.image_url != nil { + C.free(unsafe.Pointer(article.image_url)) + } + if article.favicon_url != nil { + C.free(unsafe.Pointer(article.favicon_url)) + } + if article.content != nil { + C.free(unsafe.Pointer(article.content)) + } + if article.text_content != nil { + C.free(unsafe.Pointer(article.text_content)) + } + if article.language != nil { + C.free(unsafe.Pointer(article.language)) + } + if article.published_time != nil { + C.free(unsafe.Pointer(article.published_time)) + } +} + +//export enforce_binding +func enforce_binding() {} + +func main() { + +}