Makdown document conversion method, device, equipment and storage medium
By obtaining the document unified resource locator of web articles, downloading document data and loading it into memory, extracting image links, generating image download tasks, using Ctrip technology to download pictures asynchronously, and replacing the initial image unified resource locator with the target image unified resource locator, completing the makdown document conversion, solving the problems of low conversion efficiency and error-prone in the existing technology, and achieving fast and accurate makdown document conversion.
Patent Information
- Application Number
- CN202010609662.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-06-29
- Publication Date
- 2025-05-02
- Estimated Expiration
- 2040-06-29
AI Technical Summary
The prior art when converting web articles into local articles and typing them, the process is time-consuming, labor-intensive, inefficient and error-prone.
By obtaining the document unified resource locator for web articles, downloading document data and loading it into memory, extracting image links, generating image download tasks, using Ctrip technology to download pictures asynchronously, and replacing the initial image unified resource locator with the target image unified resource locator to complete the makdown document conversion.
It realizes the rapid and accurate conversion of web articles into makdown documents, automatically recognizes and batch replaces image links, and improves the efficiency of makdown document conversion.
Smart Images

Figure CN113935286B_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present invention relate to computer technology, and in particular to a makdown document conversion method, device, equipment and storage medium. Background Art
[0002] With the advancement of electronic office, there is often a need to copy and format documents in daily work. For example, convert a web article into a local article and format it.
[0003] In view of this situation, the conventional processing method is to download the webpage article, copy the article content, and manually organize the pasted article content to achieve the required document layout. This process is time-consuming and labor-intensive, not only inefficient, but also very error-prone. Summary of the invention
[0004] The embodiments of the present invention provide a makdown document conversion method, apparatus, device and storage medium to achieve more accurate and rapid conversion of document data to be sorted into a makdown document in a text format.
[0005] In a first aspect, an embodiment of the present invention provides a makdown document conversion method, comprising:
[0006] Obtaining the document uniform resource locator of the document to be sorted, and using the document uniform resource locator as an input parameter, calling a document download function to obtain document data and load it into the memory;
[0007] Taking the document data as input parameters, calling a picture link acquisition function, extracting an initial picture uniform resource locator of each picture from the document data, and storing each initial picture uniform resource locator in a picture information set;
[0008] Traversing the picture information set, taking the picture download function, the currently traversed initial picture uniform resource locator and the document data as input parameters, calling the picture download task generation function, passing the currently traversed initial picture uniform resource locator and the document data as function parameters into the picture download function, generating a picture download task object, and storing the picture download task object in the download task set;
[0009] Taking the download task set as an input parameter, calling the Ctrip start task function, opening and executing each of the picture download task objects in the download task set in a Ctrip manner, asynchronously calling the picture download function of each of the picture download task objects, and batch downloading the pictures;
[0010] With the document data as input parameters, the image conversion function is called to replace each of the initial image uniform resource locators in the document data with the corresponding target image uniform resource locator to complete the makdown document conversion, wherein the target image uniform resource locator is a new image uniform resource locator obtained by transferring the image corresponding to the corresponding initial image uniform resource locator to the makdown server.
[0011] In a second aspect, an embodiment of the present invention further provides a makdown document conversion device, the device comprising:
[0012] The document data acquisition module is used to obtain the document uniform resource locator of the document to be sorted, and use the document uniform resource locator as an input parameter to call the document download function to obtain the document data and load it into the memory;
[0013] A picture information set generation module, used to take the document data as an input parameter, call a picture link acquisition function, extract the initial picture uniform resource locator of each picture from the document data, and store each initial picture uniform resource locator into a picture information set;
[0014] A download task set generation module is used to traverse the picture information set, take the picture download function, the initial picture uniform resource locator currently traversed and the document data as input parameters, call the picture download task generation function, pass the initial picture uniform resource locator currently traversed and the document data as function parameters into the picture download function, generate a picture download task object, and store the picture download task object in the download task set;
[0015] A picture download module, which is used to call the Ctrip start task function with the download task set as an input parameter, start and execute each of the picture download task objects in the download task set in a Ctrip manner, and asynchronously call the picture download function of each of the picture download task objects to batch download the pictures;
[0016] The image conversion module is used to call the image conversion function with the document data as input parameters, replace each of the initial image uniform resource locators in the document data with the corresponding target image uniform resource locator, and complete the makdown document conversion, wherein the target image uniform resource locator is a new image uniform resource locator obtained by transferring the image corresponding to the corresponding initial image uniform resource locator to the makdown server.
[0017] In a third aspect, an embodiment of the present invention further provides an electronic device, the electronic device comprising:
[0018] one or more processors;
[0019] a storage device for storing one or more programs,
[0020] When the one or more programs are executed by the one or more processors, the one or more processors implement the makdown document conversion method provided by any embodiment of the present invention.
[0021] In a fourth aspect, an embodiment of the present invention further provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the makdown document conversion method provided by any embodiment of the present invention.
[0022] The embodiment of the present invention obtains the document uniform resource locator of the document to be sorted, and uses the document uniform resource locator as an input parameter to call a document download function to obtain document data and load it into a memory; uses the document data as an input parameter to call a picture link acquisition function to extract the initial picture uniform resource locator of each picture from the document data, and stores each initial picture uniform resource locator in a picture information set; traverses the picture information set, uses the picture download function, the current traversed initial picture uniform resource locator and the document data as input parameters, calls a picture download task generation function, passes the current traversed initial picture uniform resource locator and the document data as function parameters into the picture download function, and generates a picture download task pair. The image download task object is stored in the download task collection; the download task collection is used as an input parameter, and the Ctrip start task function is called to open and execute each image download task object in the download task collection in accordance with the Ctrip method, and the image download function of each image download task object is asynchronously called to batch download each image; the document data is used as an input parameter, and the image conversion function is called to replace each initial image uniform resource locator in the document data with the corresponding target image uniform resource locator to complete the makdown document conversion, wherein the target image uniform resource locator is a new image uniform resource locator obtained by transferring the image corresponding to the corresponding initial image uniform resource locator to the makdown server. The automatic conversion of a webpage article into a makdown document with a certain text format is realized, and the image links in the makdow document can be automatically and batch-identified and replaced, thereby improving the efficiency of makdown document conversion. BRIEF DESCRIPTION OF THE DRAWINGS
[0023] Figure 1 It is a flowchart of a makdown document conversion method in Embodiment 1 of the present invention;
[0024] Figure 2 It is a flow chart of a method for implementing image downloading and image conversion in a makdown document conversion method in Embodiment 2 of the present invention;
[0025] Figure 3 It is a structural schematic diagram of a makdown document conversion device in Embodiment 3 of the present invention;
[0026] Figure 4 It is a structural schematic diagram of an electronic device in Embodiment 4 of the present invention. DETAILED DESCRIPTION
[0027] The present invention will be further described in detail below in conjunction with the accompanying drawings and embodiments. It is to be understood that the specific embodiments described herein are only used to explain the present invention, rather than to limit the present invention. It should also be noted that, for ease of description, only parts related to the present invention, rather than all structures, are shown in the accompanying drawings.
[0028] Embodiment 1
[0029] The makdown document conversion method provided in this embodiment can be applied to automatically convert webpage articles containing pictures into makdown documents. The method can be executed by a makdown document conversion device, which can be implemented by software and / or hardware, and the device can be integrated in an electronic device, such as a mobile phone, a tablet computer, a desktop computer or a server. Figure 1 , the method of this embodiment specifically includes:
[0030] S110: Obtain the document uniform resource locator of the document to be sorted, and use the document uniform resource locator as an input parameter to call a document download function to obtain document data and load it into the memory.
[0031] The document to be organized refers to an article that needs to be converted and formatted, such as a web page article. The document uniform resource locator (URL) refers to the network address of the web page corresponding to the document to be organized. The document download function refers to a function that automatically downloads the entire document from the document URL, which is any article download function in the related art.
[0032] Specifically, before converting the format of the document to be sorted, it is necessary to first obtain the document URL of the document to be sorted, for example, it can be manually input by the user, or automatically captured by the user triggering the document conversion operation. Then, the document download function _down_article() is called with the document URL (such as article_url) as the function input parameter, that is, _down_article(article_url). Through the execution of the document download function, the entire document can be downloaded from the corresponding address of the network and loaded into the local memory that performs the makdown document conversion operation. The document to be sorted in the memory is the document data article_file. Markdown here is a lightweight markup language that allows people to write documents in a plain document format that is easy to read and write.
[0033] S120 , taking the document data as an input parameter, calling the picture link acquisition function, extracting the initial picture uniform resource locator of each picture from the document data, and storing each initial picture uniform resource locator in the picture information set.
[0034] The image link acquisition function is a function for acquiring the network storage link of the image in the document to be sorted, which is a custom function. The initial image uniform resource locator is the storage address link of the image in the network before the document format conversion. The image information set is a set of stored image information, such as a list, and the image information here refers to the initial image uniform resource locator.
[0035] Specifically, the document data article_file is used as the function input parameter, and the custom image link acquisition function get_img_list_from_article_file() is called. The function of this function is to find and extract the initial image uniform resource locator of each image in the document to be sorted from the document data, and store all the initial image uniform resource locators into the image information set match_list. The function call code can be exemplified as: match_list = get_img_list_from_article_file(article_file).
[0036] Exemplarily, the image link acquisition function implements the function of extracting the initial image uniform resource locator of each image from the document data and storing each initial image uniform resource locator in the image information set in the following manner: defining a regular matching variable according to the image tag information in the makdown; based on the regular matching variable, calling the string matching function to match the image tag of the image uniform resource locator of the document data, and storing the matching result in the image information set, wherein the matching result is the initial image uniform resource locator of each image.
[0037] The image tag information refers to information that can indicate that a string is an image link, for example, it can be! [](url) or <img src=″> Etc. Regular expression matching variables are variables that define regular expression matching rules. String matching functions are functions that perform regular expression matching on strings.
[0038] Specifically, the process of extracting the initial image URL from the document data is as follows: First, open the document data. For example, by calling the document opening function with open(article_file,'r', encoding = 'utf-8') as f:, the document data article_file is opened in a read-only mode ("r"), and the encoding of the open file is specified, and the opened file is marked as f to effectively avoid the possibility of garbled characters. Then, define a global regular expression regular matching variable MD_IMG_PATTEN = r'! \[.*? \]\((.*?)\)|<img.*?src=[\'\"](.*?)[\'\"].*?> ', where the regular expression! \[.*? \]\((.*?)\)|<img.*?src=[\'\"](.*?)[\'\"].*?> The meaning is to match the image tag information! [](url) or <img src=″> . Finally, the regular matching variable MD_IMG_PATTEN is used as the function input parameter, and the string matching function is called, such as match_list = re.compile(MD_IMG_PATTEN).findall(f.read()), to implement string matching according to the above regular expression rules. The matching object is the file information read by the f.read() method, that is, the document data. The matching result is the initial image URL, and the initial image URL obtained by matching is then stored in the pre-defined image information set match_list. The advantage of this setting is that through the complete regular matching rules, all the initial image URLs in the document data can be located and extracted more quickly and accurately, further improving the efficiency of image link replacement.
[0039] S130, traverse the image information collection, take the image download function, the current traversal initial image uniform resource locator and document data as input parameters, call the image download task generation function, pass the current traversal initial image uniform resource locator and document data as function parameters into the image download function, generate an image download task object, and store the image download task object in the download task collection.
[0040] The image download function is a custom function for downloading images from the Internet. The image download task generation function is a custom function for generating image download tasks. The image download task object is an object that carries the image download task. The image download task is an image download function with parameters. The download task set is a predefined set that stores image download task objects, and its definition and initialization code example is g_img_task_list=[].
[0041] Specifically, when replacing the image link during the makdown document conversion process, you need to first download the image, then upload the image to the server corresponding to the makdown document, and then obtain a new image link, and replace the image link with the corresponding image position in the document data. Downloading images requires the initial image URL of each image. Therefore, in the process of batch downloading images, you must first construct the corresponding image download task object based on each initial image URL in the image information set.
[0042] In the specific implementation, first, the validity of the image information set match_list is judged by the conditional judgment statement if match_list and len(match_list)>0. That is, if the image information set match_list is not empty and the number of elements is greater than 0, then the match_list is considered valid and subsequent operations are performed; otherwise, it means that the initial image URL is not stored in the image information set match_list, which further indicates that there is no image in the document data, and there is no need to perform the subsequent steps of replacing the image link. Then, if the match_list is valid, each initial image URL in the image information set match_list is traversed through the for loop statement (such as for image_url inmatch_list).
[0043] The internal implementation of the loop body is as follows: for a certain initial image URL (the serial number in the image information set is i), the image download function _download_image, the initial image URL (image_url[i]) and the document data article_file are used as input parameters to call the image download task generation function gevent.spawn(), that is, image_task = gevent.spawn(_download_image, image_url[i], article_file). The image download task generation function asynchronously passes the initial image URL (image_url[i]) and the document data article_file as function input parameters to the image download function _download_image, and obtains an image download function with parameters as the image download task object image_task. After that, by calling the element addition function append(), that is, g_img_task_list.append(image_task), the image download task object image_task is stored in the download task set g_img_task_list. The above-mentioned internal operation of the loop body is executed for each initial image URL, and a download task set containing image download task objects of all images can be generated.
[0044] It should be noted that the image download task generation function gevent.spawn() will asynchronously call the image download function _download_image, but it only transmits input parameters to it and does not execute the image download function (i.e., the image download task object) after the parameters are passed in.
[0045] S140, taking the download task set as an input parameter, calling the Ctrip start task function, opening and executing each picture download task object in the download task set in accordance with the Ctrip method, asynchronously calling the picture download function of each picture download task object, and batch downloading each picture.
[0046] Among them, the Ctrip start task function refers to a custom function that uses the Ctrip technology to start the image download task.
[0047] Specifically, the related art has two ways of downloading pictures: downloading one by one according to the picture URL. If there are many pictures, the download efficiency of this method is very low; adopting multi-thread downloading, although the download efficiency is improved, multi-thread downloading will increase the system resource overhead, resulting in excessive system resource overhead. Based on this, the Ctrip technology is used in this embodiment to increase the download speed and reduce the system overhead.
[0048] In the specific implementation, first use the if judgment statement (such as if len(g_img_task_list)>0) to determine whether the download task set g_img_task_list is valid. If the set length is not greater than 0, it means that there is no image download task object in the download task set, and the process is terminated. If the set length is greater than 0, it means that the download task set is valid, and the image download task object is stored in it. At this time, the download task set g_img_task_list is used as an input parameter to call the Ctrip startup task function gevent.joinall(), that is, gevent.joinall(g_img_task_list). Through the execution of the Ctrip startup task function gevent.joinall(), the execution of each image download task object will be asynchronously triggered, that is, each image download function with parameters _download_image(image_url,article_file) will be asynchronously called, and each image download function will be executed to batch download the images in the document to be sorted.
[0049] S150, using the document data as an input parameter, calling the image conversion function, replacing each initial image uniform resource locator in the document data with a corresponding target image uniform resource locator, and completing the makdown document conversion.
[0050] The image conversion function refers to a custom function for converting each image link in a makdown document, which is used to upload images and replace image links. The target image uniform resource locator is a new image uniform resource locator obtained by transferring the image corresponding to the corresponding initial image uniform resource locator to the makdown server.
[0051] Specifically, after downloading all the images, the document data article_file is used as the input parameter to call the image conversion function replace_md_url(), i.e. replace_md_url(article_file). Through the execution of this function, the target image URL of each image in the makdown server can be obtained, and it can be used to replace the initial image URL corresponding to the corresponding image in the document data. When all the image URLs are replaced, the document to be collated is completely converted into a makdown document.
[0052] It should be noted that the entire process of S110 to S150 can be encapsulated into a function, such as defget_article(article_url). The function get_article() takes the document URL (article_url) of the document to be organized as an input parameter, and through the execution of the function, the document to be organized can be automatically and completely converted into a makdown document at one time.
[0053] The technical solution of this embodiment obtains the document uniform resource locator of the document to be sorted, and uses the document uniform resource locator as an input parameter to call the document download function to obtain the document data and load it into the memory; uses the document data as an input parameter to call the picture link acquisition function to extract the initial picture uniform resource locator of each picture from the document data, and stores each initial picture uniform resource locator in a picture information set; traverses the picture information set, uses the picture download function, the current traversed initial picture uniform resource locator and the document data as input parameters, calls the picture download task generation function, passes the current traversed initial picture uniform resource locator and the document data as function parameters to the picture download function, and generates a picture download task. The system uses the download task set as an input parameter, and stores the image download task object in the download task set; uses the download task set as an input parameter, calls the Ctrip start task function, opens and executes each image download task object in the download task set in accordance with the Ctrip method, and asynchronously calls the image download function of each image download task object to batch download each image; uses the document data as an input parameter, calls the image conversion function, replaces each initial image uniform resource locator in the document data with the corresponding target image uniform resource locator, and completes the makdown document conversion, wherein the target image uniform resource locator is a new image uniform resource locator obtained by transferring the image corresponding to the corresponding initial image uniform resource locator to the makdown server. The system realizes the automatic conversion of a webpage article into a makdown document with a certain text format, and can automatically and batch identify and replace image links in makdow documents, thereby improving the efficiency of makdown document conversion.
[0054] Embodiment 2
[0055] Based on the above embodiment 1, this embodiment further optimizes the "picture download function". On this basis, the "picture conversion function" can be further optimized. The explanations of the terms that are the same or corresponding to the above embodiments are not repeated here.
[0056] See also Figure 2 The implementation method of image downloading and image conversion in the makdown document conversion method provided in this embodiment includes:
[0057] S210: If the initial picture uniform resource locator in the input parameter of the picture download function is not empty, then generate a picture file corresponding to the initial picture uniform resource locator.
[0058] Specifically, for a certain image download task object in the download task set, the execution process of downloading an image is as follows: First, the initial image URL (image_url) in the input parameter of the image download function is judged to be valid through an if judgment statement (such as if img_url is None:return). If image_url is empty, it returns directly without downloading the image. If image_url is not empty, a picture file that can uniquely represent the picture corresponding to the initial picture URL is generated based on the initial picture URL (image_url).
[0059] Exemplarily, generating an image file corresponding to a corresponding initial image uniform resource locator includes: taking document data as an input parameter, calling an image path acquisition function, and obtaining image basic path information of the image in the document data; taking the initial image uniform resource locator in the input parameter of the image download function as an input function, calling an image name acquisition function, and obtaining image name information corresponding to the corresponding initial image uniform resource locator; and generating an image file corresponding to the corresponding initial image uniform resource locator based on the image basic path information, image name information, and image suffix information.
[0060] The image path acquisition function refers to a function that can acquire the basic path information of the image stored on the network. The image name acquisition function refers to a function that acquires the image name.
[0061] Specifically, with the document data article_file as the input parameter, the image path acquisition function article_file_image_path() is called, that is, article_file_image_path(article_file). Through the execution of the function, the basic path information of the image corresponding to the initial image URL (image_url) can be obtained. Then, with the initial image URL (image_url) in the input parameter as the input function, the image name acquisition function is called, and the code example is: os.sep+get_name_from_url(img_url) to obtain the image name information corresponding to the corresponding initial image URL (image_url). Finally, the image basic path information, image name information and image suffix information (such as ".jpg") are combined to obtain the image file img_file object corresponding to the initial image URL (image_url). The code implementation example of the above process is: img_file=article_file_image_path(article_file)+os.sep+get_name_from_url(img_url)+".jpg".
[0062] S220: If it is determined based on the picture file that the picture file exists in the local cache space, the picture file in the local cache space is deleted.
[0063] Specifically, in order to avoid errors in pictures and picture links, the present embodiment takes into account the situation of different pictures with the same picture file information. Therefore, after obtaining the picture file, it is necessary to determine whether the picture file exists in the local cache space. If the picture file exists in the local cache space, delete it and then execute S230. For example, through the sample code ifremove_cache and os.path.exists(img_file):os.remove(img_file), the judgment and deletion of the picture file in the local cache space are implemented. If the picture file does not exist in the local cache space, it is necessary to download the picture and execute S230.
[0064] S230, taking the initial picture uniform resource locator in the input parameter of the picture download function as an input parameter, calling the picture acquisition function, and obtaining the picture data stream corresponding to the initial picture uniform resource locator from the server corresponding to the document to be sorted.
[0065] Among them, the image acquisition function refers to the function of pulling images from the network.
[0066] Specifically, the image acquisition function requests_get() is called with the initial image URL (image_url) as an input parameter, that is, img_stream=requests_get(image_url). Through the execution of the function, the image data stream information (i.e., image data stream) is obtained from the network address of image_url through a get request and marked as img_stream.
[0067] S240. If the picture data stream is not empty, call the file writing function to write the picture data stream into the picture file to complete the picture download operation.
[0068] Specifically, if the image data stream is judged to be an empty object, it means that there is no image information, and the process is terminated. If the image data stream is judged not to be an empty object, it means that there is real and valid image information in the image data stream. Then the image data stream is converted into an image file. In specific implementation, the image file img_file is used as the input function, and the file open function is called, that is, with open(img_file,'wb')as f, to open a file handle and mark it as f. Then, the image data stream is used as the input function, and the file write function is called, that is, f.write(img_stream.content), to store the content in the image data stream img_stream into the f file, and the download operation of the image is completed.
[0069] For each picture download task object in the download task set, S210 to S240 are executed asynchronously, so that all pictures can be downloaded in batches.
[0070] S250: Obtain a picture information set based on the document data, and convert the picture information set into a scale object.
[0071] Specifically, the function input parameter of the image conversion function is the document data article_file. Based on this, during the image replacement process, the post=article_file.read() function is first called to read the content in the document data article_file and mark it as post. Then, the image information set image_list is obtained by calling get_img_list_from_md_file(article_file). And, with the image information set as the input parameter, the image_list list is converted into a scale group object by calling the list(chain(*image_list)) method.
[0072] S260. For each initial image uniform resource locator in the scale object, if it is determined that the initial image uniform resource locator is not empty and contains the "http" field, then the document data and the initial image uniform resource locator are used as input parameters, and the image link conversion function is called to obtain the target image uniform resource locator corresponding to the initial image uniform resource locator. The image link replacement function is called with the initial image uniform resource locator and the target image uniform resource locator as input parameters to replace the initial image uniform resource locator in the document data with the target image uniform resource locator, thereby completing the content conversion of the image corresponding to the initial image uniform resource locator in the document data.
[0073] Specifically, each initial image URL in the gauge object is traversed through a for loop statement to replace the image links one by one. For any initial image URL in the gauge object, the following image link replacement operation is performed:
[0074] First, through the if judgment statement, the sample code is: if image_url and len(image_url)>0and"http"in image_url, to determine whether the initial image URL is an empty object, whether the string length is greater than 0, and whether the string contains the "http" string. If any of the conditions is not met, the process ends. If all three conditions are met, it means that the initial image URL is the real information of an http type network image. At this time, with the document data article_file and the initial image uniform resource locator image_url as input parameters, call the image link conversion function image_url_convert(), that is, new_url=image_url_convert(md_file,image_url). Through the execution of the function, the address conversion logic of the image is realized, and the target image uniform resource locator new_url corresponding to the initial image uniform resource locator image_url is obtained. Then, when it is determined that the target image uniform resource locator new_url is not an empty object, the image link replacement function post.replace() is called with the initial image uniform resource locator image_url and the target image uniform resource locator new_url as input parameters, and the image_url in the document data article_file is replaced with new_url, completing the content conversion of the image corresponding to the initial image URL in the document data.
[0075] Exemplarily, the image link conversion function implements the function of obtaining the target image uniform resource locator corresponding to the initial image uniform resource locator in the following manner: uploading the image file corresponding to the initial image uniform resource locator to the makdown server so that the makdown server stores the image file; receiving the new image uniform resource locator for storing the image file returned by the makdown server as the target image uniform resource locator.
[0076] Specifically, the internal implementation of the image link conversion function image_url_convert() is: upload the image corresponding to the initial image URL to the server where the makdown service is located (i.e., the makdown server). After the makdown server stores the image, it will return a new image URL to the device that performs the document conversion based on its network address. The returned new image URL is the target image uniform resource locator corresponding to the initial image URL.
[0077] The technical solution of this embodiment is as follows: if the initial picture uniform resource locator in the input parameter of the picture download function is not empty, then the picture file corresponding to the corresponding initial picture uniform resource locator is generated; with the initial picture uniform resource locator in the input parameter of the picture download function as the input parameter, the picture acquisition function is called to obtain the picture data stream corresponding to the corresponding initial picture uniform resource locator from the server corresponding to the document to be sorted; if the picture data stream is not empty, the file writing function is called to write the picture data stream into the picture file to complete the picture download operation. The fast download of the picture file is realized, thereby further improving the picture conversion speed. If it is determined based on the picture file that the picture file exists in the local cache space, the picture file in the local cache space is deleted. The deletion of the picture files with the same picture file information in the local cache space is realized, avoiding the confusion problem of different pictures with the same picture file information, and further improving the accuracy of the picture conversion. By converting the picture information set into a scale object; for each initial picture uniform resource locator in the scale object, if it is determined that the initial picture uniform resource locator is not empty and contains the "http" field, the picture link conversion function is called with the document data and the initial picture uniform resource locator as input parameters to obtain the target picture uniform resource locator corresponding to the initial picture uniform resource locator, and the picture link replacement function is called with the initial picture uniform resource locator and the target picture uniform resource locator as input parameters to replace the initial picture uniform resource locator in the document data with the target picture uniform resource locator, thus completing the content conversion of the picture corresponding to the initial picture uniform resource locator in the document data. The batch automatic replacement of picture links in the process of makdown document conversion is realized, further improving the accuracy and efficiency of picture link replacement.
[0078] Embodiment 3
[0079] This embodiment provides a makdown document conversion device. Figure 3 , the device specifically comprises:
[0080] The document data acquisition module 310 is used to obtain the document uniform resource locator of the document to be sorted, and use the document uniform resource locator as an input parameter to call the document download function to obtain the document data and load it into the memory;
[0081] The picture information set generation module 320 is used to use the document data as an input parameter, call the picture link acquisition function, extract the initial picture uniform resource locator of each picture from the document data, and store each initial picture uniform resource locator in the picture information set;
[0082] The download task set generation module 330 is used to traverse the picture information set, take the picture download function, the current traversal initial picture uniform resource locator and document data as input parameters, call the picture download task generation function, pass the current traversal initial picture uniform resource locator and document data as function parameters into the picture download function, generate a picture download task object, and store the picture download task object in the download task set;
[0083] The picture download module 340 is used to call the Ctrip start task function with the download task set as an input parameter, start and execute each picture download task object in the download task set in the Ctrip mode, and asynchronously call the picture download function of each picture download task object to batch download each picture;
[0084] The image conversion module 350 is used to call the image conversion function with the document data as input parameters, replace each initial image uniform resource locator in the document data with the corresponding target image uniform resource locator, and complete the makdown document conversion, wherein the target image uniform resource locator is a new image uniform resource locator obtained by transferring the image corresponding to the corresponding initial image uniform resource locator to the makdown server.
[0085] Optionally, based on the above device, the device further includes a picture link acquisition function execution module, which is used to extract the initial picture uniform resource locator of each picture from the document data and store each initial picture uniform resource locator in the picture information set in the following manner:
[0086] Define regular matching variables based on the image tag information in makdown;
[0087] Based on the regular matching variable, the string matching function is called to match the image tag of the image uniform resource locator of the document data, and the matching result is stored in the image information set, wherein the matching result is the initial image uniform resource locator of each image.
[0088] Optionally, based on the above device, the device further includes a picture download function execution module, which is used to implement the function of downloading pictures in the following manner:
[0089] If the initial picture uniform resource locator in the input parameter of the picture download function is not empty, then generate a picture file corresponding to the initial picture uniform resource locator;
[0090] Taking the initial picture uniform resource locator in the input parameter of the picture download function as the input parameter, calling the picture acquisition function, and obtaining the picture data stream corresponding to the initial picture uniform resource locator from the server corresponding to the document to be sorted;
[0091] If the image data stream is not empty, the file writing function is called to write the image data stream into the image file to complete the image download operation.
[0092] Furthermore, the picture download function execution module is specifically used for:
[0093] With the document data as input parameter, call the image path acquisition function to obtain the basic image path information of the image in the document data;
[0094] Taking the initial picture uniform resource locator in the input parameter of the picture download function as the input function, calling the picture name acquisition function to obtain the picture name information corresponding to the initial picture uniform resource locator;
[0095] Generate a picture file corresponding to the corresponding initial picture uniform resource locator according to the picture basic path information, picture name information and picture suffix information.
[0096] Furthermore, the picture download function execution module is also used to:
[0097] After the image file corresponding to the corresponding initial image uniform resource locator is generated, if it is determined based on the image file that the image file exists in the local cache space, the image file in the local cache space is deleted.
[0098] Optionally, based on the above device, the device further includes a picture conversion function execution module, which is used to implement the function of replacing each initial picture uniform resource locator in the document data with a corresponding target picture uniform resource locator in the following manner:
[0099] Obtain a picture information set based on the document data, and convert the picture information set into a scale object;
[0100] For each initial image uniform resource locator in the scale object, if it is determined that the initial image uniform resource locator is not empty and contains the "http" field, then the document data and the initial image uniform resource locator are used as input parameters, and the image link conversion function is called to obtain the target image uniform resource locator corresponding to the initial image uniform resource locator. The image link replacement function is called with the initial image uniform resource locator and the target image uniform resource locator as input parameters to replace the initial image uniform resource locator in the document data with the target image uniform resource locator, thereby completing the content conversion of the image corresponding to the initial image uniform resource locator in the document data.
[0101] Furthermore, based on the above device, the device further includes a picture link conversion function execution module, which is used to implement the function of obtaining the target picture uniform resource locator corresponding to the initial picture uniform resource locator in the following manner:
[0102] Upload the image file corresponding to the initial image uniform resource locator to the makdown server so that the makdown server stores the image file;
[0103] Receive the new picture uniform resource locator of the storage picture file returned by the makdown server as the target picture uniform resource locator.
[0104] Through a makdown document conversion device of embodiment 3 of the present invention, it is possible to automatically convert a web article into a makdown document with a certain text format, and can automatically and batch-recognize and replace image links in makdow documents, thereby improving the efficiency of makdown document conversion.
[0105] The makdown document conversion device provided in the embodiment of the present invention can execute the makdown document conversion method provided in any embodiment of the present invention, and has the corresponding functional modules and beneficial effects of the execution method.
[0106] It is worth noting that in the embodiment of the above-mentioned makdown document conversion device, the various units and modules included are only divided according to functional logic, but are not limited to the above-mentioned division, as long as the corresponding functions can be achieved; in addition, the specific names of the functional units are only for the convenience of distinguishing each other, and are not used to limit the scope of protection of the present invention.
[0107] Embodiment 4
[0108] See also Figure 4This embodiment provides an electronic device 400, which includes: one or more processors 420; a storage device 410, which is used to store one or more programs. When the one or more programs are executed by the one or more processors 420, the one or more processors 420 implement the makdown document conversion method provided by the embodiment of the present invention, including:
[0109] Obtain the document uniform resource locator of the document to be sorted, and use the document uniform resource locator as an input parameter to call the document download function to obtain the document data and load it into the memory;
[0110] Taking the document data as input parameters, calling the picture link acquisition function, extracting the initial picture uniform resource locator of each picture from the document data, and storing each initial picture uniform resource locator into the picture information set;
[0111] Traverse the image information collection, take the image download function, the current traversal initial image uniform resource locator and document data as input parameters, call the image download task generation function, pass the current traversal initial image uniform resource locator and document data as function parameters to the image download function, generate an image download task object, and store the image download task object in the download task collection;
[0112] With the download task set as input parameter, the Ctrip start task function is called, each image download task object in the download task set is opened and executed in the Ctrip mode, and the image download function of each image download task object is asynchronously called to batch download each image;
[0113] With document data as input parameter, call the image conversion function, replace each initial image uniform resource locator in the document data with the corresponding target image uniform resource locator, and complete the makdown document conversion, wherein the target image uniform resource locator is a new image uniform resource locator obtained by transferring the image corresponding to the corresponding initial image uniform resource locator to the makdown server.
[0114] Of course, those skilled in the art can understand that the processor 420 can also implement the technical solution of the makdown document conversion method provided by any embodiment of the present invention.
[0115] Figure 4 The electronic device 400 shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present invention.
[0116] like Figure 4As shown, the electronic device 400 is in the form of a general-purpose computing device. The components of the electronic device 400 may include, but are not limited to: one or more processors 420, a storage device 410, and a bus 450 connecting different system components (including the storage device 410 and the processor 420).
[0117] Bus 450 represents one or more of several types of bus structures, including a memory bus or memory controller, a peripheral bus, an accelerated graphics port, a processor or a local bus using any of a variety of bus architectures. By way of example, these architectures include, but are not limited to, an Industry Standard Architecture (ISA) bus, a Micro Channel Architecture (MAC) bus, an Enhanced ISA bus, a Video Electronics Standards Association (VESA) local bus, and a Peripheral Component Interconnect (PCI) bus.
[0118] The electronic device 400 typically includes a variety of computer system readable media. These media can be any available media that can be accessed by the electronic device 400, including volatile and non-volatile media, removable and non-removable media.
[0119] The storage device 410 may include computer system readable media in the form of volatile memory, such as random access memory (RAM) 411 and / or cache memory 412. The electronic device 400 may further include other removable / non-removable, volatile / non-volatile computer system storage media. By way of example only, the storage system 413 may be used to read and write non-removable, non-volatile magnetic media ( Figure 4 not shown, usually called a "hard drive"). Although Figure 4 Not shown in the figure, a disk drive for reading and writing to a removable non-volatile disk (e.g., a "floppy disk"), and an optical disk drive for reading and writing to a removable non-volatile optical disk (e.g., a CD-ROM, a DVD-ROM, or other optical media) may be provided. In these cases, each drive may be connected to the bus 450 via one or more data medium interfaces. The storage device 410 may include at least one program product having a set (e.g., at least one) of program modules that are configured to perform the functions of the embodiments of the present invention.
[0120] A program / utility 414 having a set (at least one) of program modules 415 may be stored, for example, in a storage device 410, such program modules 415 including but not limited to an operating system, one or more application programs, other program modules, and program data, each of which or some combination may include an implementation of a network environment. The program modules 415 generally perform the functions and / or methods of any embodiment described herein.
[0121] The electronic device 400 may also communicate with one or more external devices 460 (e.g., keyboard, pointing device, display 470, etc.), one or more devices that enable a user to interact with the electronic device 400, and / or any device that enables the electronic device 400 to communicate with one or more other computing devices (e.g., network card, modem, etc.). Such communication may be performed through an input / output interface (I / O interface) 430. Furthermore, the electronic device 400 may also communicate with one or more networks (e.g., local area network (LAN), wide area network (WAN) and / or public network, such as the Internet) through a network adapter 440. Figure 4 As shown, the network adapter 440 communicates with other modules of the electronic device 400 via the bus 450. It should be understood that, although not shown in the figure, other hardware and / or software modules can be used in conjunction with the electronic device 400, including but not limited to: microcode, device drivers, redundant processing units, external disk drive arrays, RAID systems, tape drives, and data backup storage systems.
[0122] The processor 420 executes various functional applications and data processing by running the programs stored in the storage device 410, such as implementing the makdown document conversion method provided in the embodiment of the present invention.
[0123] Embodiment 5
[0124] This embodiment provides a storage medium containing computer executable instructions. When the computer executable instructions are executed by a computer processor, they are used to perform a makdown document conversion method. The method includes:
[0125] Obtain the document uniform resource locator of the document to be sorted, and use the document uniform resource locator as an input parameter to call the document download function to obtain the document data and load it into the memory;
[0126] Taking the document data as input parameters, calling the picture link acquisition function, extracting the initial picture uniform resource locator of each picture from the document data, and storing each initial picture uniform resource locator into the picture information set;
[0127] Traverse the image information collection, take the image download function, the current traversal initial image uniform resource locator and document data as input parameters, call the image download task generation function, pass the current traversal initial image uniform resource locator and document data as function parameters to the image download function, generate an image download task object, and store the image download task object in the download task collection;
[0128] With the download task set as input parameter, the Ctrip start task function is called, each image download task object in the download task set is opened and executed in the Ctrip mode, and the image download function of each image download task object is asynchronously called to batch download each image;
[0129] With document data as input parameter, call the image conversion function, replace each initial image uniform resource locator in the document data with the corresponding target image uniform resource locator, and complete the makdown document conversion, wherein the target image uniform resource locator is a new image uniform resource locator obtained by transferring the image corresponding to the corresponding initial image uniform resource locator to the makdown server.
[0130] Of course, the storage medium containing computer executable instructions provided in an embodiment of the present invention is not limited to the above method operations, and the computer executable instructions can also execute related operations in the makdown document conversion method provided in any embodiment of the present invention.
[0131] The computer storage medium of the embodiment of the present invention can adopt any combination of one or more computer-readable media. The computer-readable medium can be a computer-readable signal medium or a computer-readable storage medium. The computer-readable storage medium can be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples (non-exhaustive list) of computer-readable storage media include: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In this document, a computer-readable storage medium can be any tangible medium containing or storing a program, which can be used by an instruction execution system, device or device or used in combination with it.
[0132] Computer-readable signal media may include data signals propagated in baseband or as part of a carrier wave, which carry computer-readable program code. Such propagated data signals may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. Computer-readable signal media may also be any computer-readable medium other than a computer-readable storage medium, which may send, propagate, or transmit a program for use by or in conjunction with an instruction execution system, apparatus, or device.
[0133] Program code embodied on a computer readable medium may be transmitted using any appropriate medium, including but not limited to wireless, wireline, optical fiber cable, RF, etc., or any suitable combination of the foregoing.
[0134] Computer program code for performing the operations of the present invention may be written in one or more programming languages or a combination thereof, including object-oriented programming languages such as Java, Smalltalk, C++, and conventional procedural programming languages such as "C" or similar programming languages. The program code may be executed entirely on the user's computer, partially on the user's computer, as a separate software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In the case of a remote computer, the remote computer may be connected to the user's computer through any type of network, including a local area network (LAN) or a wide area network (WAN), or may be connected to an external computer (e.g., through the Internet using an Internet service provider).
[0135] Note that the above are only preferred embodiments of the present invention and the technical principles used. Those skilled in the art will understand that the present invention is not limited to the specific embodiments described herein, and that various obvious changes, readjustments and substitutions can be made by those skilled in the art without departing from the scope of protection of the present invention. Therefore, although the present invention has been described in more detail through the above embodiments, the present invention is not limited to the above embodiments, and may include more other equivalent embodiments without departing from the concept of the present invention, and the scope of the present invention is determined by the scope of the appended claims.
Claims
1. A makdown document conversion method, characterized in that: include: Obtaining the document uniform resource locator of the document to be sorted, and using the document uniform resource locator as an input parameter, calling a document download function to obtain document data and load it into the memory; Taking the document data as input parameters, calling a picture link acquisition function, extracting an initial picture uniform resource locator of each picture from the document data, and storing each initial picture uniform resource locator in a picture information set; Traversing the picture information set, taking the picture download function, the currently traversed initial picture uniform resource locator and the document data as input parameters, calling the picture download task generation function, passing the currently traversed initial picture uniform resource locator and the document data as function parameters into the picture download function, generating a picture download task object, and storing the picture download task object in the download task set; Taking the download task set as an input parameter, calling the Ctrip start task function, opening and executing each of the picture download task objects in the download task set in a Ctrip manner, asynchronously calling the picture download function of each of the picture download task objects, and batch downloading the pictures; With the document data as input parameters, the image conversion function is called to replace each of the initial image uniform resource locators in the document data with the corresponding target image uniform resource locator to complete the makdown document conversion, wherein the target image uniform resource locator is a new image uniform resource locator obtained by transferring the image corresponding to the corresponding initial image uniform resource locator to the makdown server.
2. The method according to claim 1, characterized in that The picture link acquisition function extracts the initial picture uniform resource locator of each picture from the document data and stores each initial picture uniform resource locator in the picture information set in the following manner: Define regular matching variables based on the image tag information in makdown; Based on the regular matching variable, a string matching function is called to match the image tag of the image uniform resource locator of the document data, and the matching result is stored in the image information set, wherein the matching result is the initial image uniform resource locator of each image.
3. The method according to claim 1, characterized in that The picture download function implements the function of downloading the picture in the following way: If the initial picture uniform resource locator in the input parameter of the picture download function is not empty, then generate a picture file corresponding to the initial picture uniform resource locator; Taking the initial picture uniform resource locator in the input parameter of the picture download function as an input parameter, calling the picture acquisition function, and obtaining the picture data stream corresponding to the initial picture uniform resource locator from the server corresponding to the document to be sorted; If the picture data stream is not empty, a file writing function is called to write the picture data stream into the picture file, thereby completing the picture download operation.
4. The method according to claim 3, characterized in that The image files corresponding to the generated initial image uniform resource locator include: Taking the document data as input parameters, calling the picture path acquisition function to obtain the picture basic path information of the picture in the document data; Taking the initial picture uniform resource locator in the input parameter of the picture download function as the input function, calling the picture name acquisition function to obtain the picture name information corresponding to the initial picture uniform resource locator; A picture file corresponding to the corresponding initial picture uniform resource locator is generated according to the picture basic path information, the picture name information and the picture suffix information.
5. The method according to claim 3, characterized in that: After generating the image file corresponding to the corresponding initial image uniform resource locator, it also includes: If it is determined based on the picture file that the picture file exists in the local cache space, the picture file in the local cache space is deleted.
6. The method according to claim 1, characterized in that The picture conversion function realizes the function of replacing each of the initial picture uniform resource locators in the document data with the corresponding target picture uniform resource locators in the following manner: Obtaining the picture information set based on the document data, and converting the picture information set into a scale object; For each initial image uniform resource locator in the scale object, if it is determined that the initial image uniform resource locator is not empty and contains the "http" field, then the document data and the initial image uniform resource locator are used as input parameters, and the image link conversion function is called to obtain the target image uniform resource locator corresponding to the initial image uniform resource locator, and the image link replacement function is called with the initial image uniform resource locator and the target image uniform resource locator as input parameters to replace the initial image uniform resource locator in the document data with the target image uniform resource locator, thereby completing the content conversion of the image corresponding to the initial image uniform resource locator in the document data.
7. The method according to claim 6, characterized in that The picture link conversion function realizes the function of obtaining the target picture uniform resource locator corresponding to the initial picture uniform resource locator in the following manner: Uploading the image file corresponding to the initial image uniform resource locator to the makdown server so that the makdown server stores the image file; A new picture uniform resource locator storing the picture file returned by the makdown server is received as the target picture uniform resource locator.
8. A makdown document conversion device, characterized in that: include: The document data acquisition module is used to obtain the document uniform resource locator of the document to be sorted, and use the document uniform resource locator as an input parameter to call the document download function to obtain the document data and load it into the memory; A picture information set generation module, used to take the document data as an input parameter, call a picture link acquisition function, extract the initial picture uniform resource locator of each picture from the document data, and store each initial picture uniform resource locator into a picture information set; A download task set generation module is used to traverse the picture information set, take the picture download function, the initial picture uniform resource locator currently traversed and the document data as input parameters, call the picture download task generation function, pass the initial picture uniform resource locator currently traversed and the document data as function parameters into the picture download function, generate a picture download task object, and store the picture download task object in the download task set; A picture download module, which is used to call the Ctrip start task function with the download task set as an input parameter, start and execute each of the picture download task objects in the download task set in a Ctrip manner, and asynchronously call the picture download function of each of the picture download task objects to batch download the pictures; The image conversion module is used to call the image conversion function with the document data as input parameters, replace each of the initial image uniform resource locators in the document data with the corresponding target image uniform resource locator, and complete the makdown document conversion, wherein the target image uniform resource locator is a new image uniform resource locator obtained by transferring the image corresponding to the corresponding initial image uniform resource locator to the makdown server.
9. An electronic device, characterized in that: The electronic device comprises: one or more processors; a storage device for storing one or more programs, When the one or more programs are executed by the one or more processors, the one or more processors implement the makdown document conversion method as described in any one of claims 1-7.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the makdown document conversion method as described in any one of claims 1-7 is implemented.
Citation Information
Patent Citations
Document auditing method, device and system, equipment and storage medium
CN110852065A
Article migration method and device, computer readable storage medium and terminal equipment
CN110909270A