markdown图片转换demo

markdown图片转换demo

一直以来都是用Markdown来写博客的,但是它的图片嵌入实在是太让人头秃,逼得我能找网上的图片就不用自己的,实在是麻烦。所以我在发现了一个可以生成markdown样式的图床后就写了一个小程序,花了2个小时左右。平常把编辑器文件夹的图片(一般是截图)上传上去,再把markdown形式的链接保存到文件里,然后进行转换。当然有很多限制,比如没有图形化界面(过一阵闲下来就去学Qt),软件必须依赖一个配置文件;没有调用图床的API而是每次需要手动上传(因为白嫖的图床,感觉不太好);需要先把要修改的文档拷贝到配置文件设定的txt文件中(还是因为不会Qt,终端里面输入我觉得太反人类了),然后生成到另一个txt文件中。之后学会Qt、找到了正规图床之后可能会改改吧。

/*
 * @Author: JiaDing 
 * @Date: 2019-10-19 19:07:15 
 * @Last Modified by: JiaDing
 * @Last Modified time: 2019-10-19 20:30:51
 */
/*
*该程序是用来处理markdown文档上传时,图片上传问题的(用于更名)
*/
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
string getPartOfString(string& input,char dot){
    string ans;
    int index=0;
    while(input[index]!=dot)
    index++;
    for(int i=index+1;i<input.length();i++){
        ans+=input[i];
    }
    return ans;
}
void readFile(string* inputTextPath,string* outputTextPath,string* outputPicturePath){
    vector<string*>saveString;
    saveString.push_back(inputTextPath);
    saveString.push_back(outputTextPath);
    saveString.push_back(outputPicturePath);
    cout << "------欢迎使用Markdown上传程序-----------\n本软件只支持类Github可生产markdown图片格式的图床\n" << endl;
    ifstream in("setPicturePath.txt");
    string tempRead;
    char tempChar;
    while(!in.eof()){
        in>>tempRead;
        cout<<tempRead<<endl;
        tempRead.clear();
    }
    in.close();
    cout<<"这是当前的配置,您要使用吗,如果不要赶快去改!\n改完了输入y确认"<<endl;
    cin>>tempChar;
    while(tempChar!='y'){
        cout<<"请输入y"<<endl;
        cin>>tempChar;
    }
    cout<<"是否已将所需文件写入指定地点,是请输入y"<<endl;
    cin>>tempChar;
    while(tempChar!='y'){
        cout<<"完成请输入y"<<endl;
        cin>>tempChar;
    }
    ifstream in2("setPicturePath.txt");
    //cout<<in2.is_open()<<endl;
    for(int i=0;i<3;i++){
        in2>>tempRead;
        saveString[i]->append(getPartOfString(tempRead,'\:'));
        cout<<"saveString[i]="<<&saveString[i]<<endl;
    }
    in2.close();
}
class afterPicture{
        public:
        string nameWithoutType;
        string fullName;
        afterPicture(string fullname,string namewithoutType){
            fullName=fullname;
            nameWithoutType=namewithoutType;
        }
    };
void readPicture(string& outputPicturePath,vector<afterPicture*>& afterPictureVector){
    ifstream in(outputPicturePath);
    string tempFullPath;
    string tempPathNoType;
    int tempIndex=0;
    while(!in.eof()){
        in>>tempFullPath;
        tempIndex=0;

        while(tempFullPath[tempIndex]!='[')
            tempIndex++;
        tempIndex++;
        while(tempFullPath[tempIndex]!=']'){
            tempPathNoType+=tempFullPath[tempIndex];
            tempIndex++;
        }
        afterPictureVector.push_back(new afterPicture(tempFullPath,tempPathNoType));
        tempFullPath.clear();
        tempPathNoType.clear();
    }
    in.close();
}
string search(string& inputPicturePath,vector<afterPicture*>&afterPictureVector){
    int length=afterPictureVector.size();
    for(int i=0;i<length;i++){
        if(inputPicturePath==afterPictureVector[i]->nameWithoutType)
        return afterPictureVector[i]->fullName;
    }
    return "(Error:This picture is not found in the List!)";
}
void readText(string& inputTextPath,string& outputTextPath,vector<afterPicture*>&afterPictureVector){
    ifstream in(inputTextPath);
    ofstream out(outputTextPath);
    char tempChar;
    string searchString;
    string searchAns;
    while(!in.eof()){
        tempChar=in.get();
         if (tempChar == '!')
        {
            tempChar = in.get();
            if (tempChar == '[')
            {
                tempChar=in.get();
                while (tempChar != ']')
                {
                    searchString+=tempChar;
                    tempChar=in.get();
                }
                searchAns=search(searchString,afterPictureVector);
                searchString.clear();
                out<<searchAns;
                while(tempChar!=')')
                    tempChar=in.get();
                tempChar=in.get();
            }
            else
            {
                out<<'!'<<tempChar;
            }
        }
        else
        {
            out<<tempChar;
        }
    }
    in.close();
    out.close();
}

int main(){
    string inputTextPath;
    string outputPicturePath;
    string outputTextPath;
    vector<afterPicture*> afterPictureVector;
    readFile(&inputTextPath,&outputTextPath,&outputPicturePath);
    readPicture(outputPicturePath,afterPictureVector);
    readText(inputTextPath,outputTextPath,afterPictureVector);
    cout<<"处理完毕!"<<endl;
    return 0;
}

配置文件长这个样子:

输入文档的地址:E://textNeedChange.txt
输出文档的地址:E://textChanged.txt
输出图片的地址:E://afterChangePicture.txt

之后可能会修改一个Linux版的,因为主力系统还是Linux。

猜你喜欢

转载自www.cnblogs.com/jiading/p/11706030.html