rasgapdf 0.2.1__tar.gz → 0.2.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: rasgapdf
3
- Version: 0.2.1
3
+ Version: 0.2.2
4
4
  Summary: Ultra-fast 2D spatial PDF parser powered by pure Rust — extract exact [x0, y0, x1, y1] bounding boxes, text blocks, tables, images & vector paths in milliseconds for RAG & LLMs
5
5
  Author: Messias Martins
6
6
  Project-URL: Homepage, https://martinsmessias.github.io/rasgapdf/
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "rasgapdf"
7
- version = "0.2.1"
7
+ version = "0.2.2"
8
8
  authors = [
9
9
  { name="Messias Martins" },
10
10
  ]
@@ -70,7 +70,7 @@ if _lib:
70
70
 
71
71
  def version() -> str:
72
72
  if not _lib:
73
- return "0.2.1 (unlinked)"
73
+ return "0.2.2 (unlinked)"
74
74
  return _lib.rasgapdf_version().decode("utf-8")
75
75
 
76
76
  def parse_raw(file_path: str) -> Dict[str, Any]:
@@ -263,47 +263,61 @@ def extract_text(data_or_path: bytes | str) -> str:
263
263
  data = None
264
264
  is_bytes = False
265
265
 
266
+ res_str = ""
266
267
  # 1. Caminho ultra-rápido nativo (se a biblioteca expor rasgapdf_extract_text_bytes)
267
268
  if _lib and hasattr(_lib, "rasgapdf_extract_text_bytes"):
268
269
  b_data = data if is_bytes else Path(data_or_path).read_bytes()
269
270
  res_ptr = _lib.rasgapdf_extract_text_bytes(b_data, len(b_data))
270
271
  if res_ptr:
271
272
  try:
272
- res_str = ctypes.string_at(res_ptr).decode("utf-8", errors="replace")
273
- if res_str.strip():
274
- return res_str
273
+ native_str = ctypes.string_at(res_ptr).decode("utf-8", errors="replace")
274
+ if native_str.strip():
275
+ res_str = native_str
275
276
  finally:
276
277
  _lib.rasgapdf_free_string(res_ptr)
277
278
 
278
279
  # 2. Fallback robusto via parse_pdf estruturado (preserva blocos e colunas)
279
- tmp_path = None
280
- try:
281
- if is_bytes:
282
- import tempfile
283
- with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as tf:
284
- tf.write(data)
285
- tmp_path = tf.name
286
- path_to_parse = tmp_path
287
- else:
288
- path_to_parse = str(data_or_path)
289
-
290
- doc = parse_pdf(path_to_parse, detect_tables=False)
291
- pages_texts = []
292
- for page in doc.get("pages", []):
293
- lines = []
294
- for block in page.get("text_blocks", []):
295
- for line in block.get("lines", []):
296
- text = line.get("text", "")
297
- if text:
298
- lines.append(text)
299
- pages_texts.append("\n".join(lines))
300
- return "\n".join(pages_texts)
301
- finally:
302
- if tmp_path and os.path.exists(tmp_path):
303
- try:
304
- os.remove(tmp_path)
305
- except Exception:
306
- pass
280
+ if not res_str:
281
+ tmp_path = None
282
+ try:
283
+ if is_bytes:
284
+ import tempfile
285
+ with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as tf:
286
+ tf.write(data)
287
+ tmp_path = tf.name
288
+ path_to_parse = tmp_path
289
+ else:
290
+ path_to_parse = str(data_or_path)
291
+
292
+ doc = parse_pdf(path_to_parse, detect_tables=False)
293
+ pages_texts = []
294
+ for page in doc.get("pages", []):
295
+ lines = []
296
+ for block in page.get("text_blocks", []):
297
+ for line in block.get("lines", []):
298
+ text = line.get("text", "")
299
+ if text and text.strip():
300
+ lines.append(text.rstrip())
301
+ if lines:
302
+ pages_texts.append("\n".join(lines))
303
+ res_str = "\n\n".join(pages_texts)
304
+ finally:
305
+ if tmp_path and os.path.exists(tmp_path):
306
+ try:
307
+ os.remove(tmp_path)
308
+ except Exception:
309
+ pass
310
+
311
+ # Normalização nativa de whitespace
312
+ if res_str:
313
+ import re
314
+ lines = [line.rstrip() for line in res_str.splitlines()]
315
+ joined = "\n".join(lines)
316
+ res_str = re.sub(r"\n{3,}", "\n\n", joined).strip()
317
+ if res_str:
318
+ res_str += "\n"
319
+
320
+ return res_str
307
321
 
308
322
 
309
323
  def extract_images_bytes(data: bytes, output_dir: str) -> Dict[str, Any]:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: rasgapdf
3
- Version: 0.2.1
3
+ Version: 0.2.2
4
4
  Summary: Ultra-fast 2D spatial PDF parser powered by pure Rust — extract exact [x0, y0, x1, y1] bounding boxes, text blocks, tables, images & vector paths in milliseconds for RAG & LLMs
5
5
  Author: Messias Martins
6
6
  Project-URL: Homepage, https://martinsmessias.github.io/rasgapdf/
File without changes
File without changes